Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rename One File In Batch To It's Modified Date

46 views
Skip to first unread message

Climate Hillbilly Davis

unread,
Jan 4, 2018, 7:52:14 PM1/4/18
to

Need to rename one file in batch to it's created date, i.e.,
"latest.gho" > "createddate.gho"

Format example: "01-04-2018.gho"

Just one file, not a group.

I use this to rename it to current date: if exist D:\Latest.gho for /f
"tokens=1-5 delims=/ " %%d in ("%date%") do rename "D:\Latest.gho" %%e-%%
f-%%g_C.gho

Thanks.

ada...@poczta.onet.pl

unread,
Jan 5, 2018, 4:47:58 AM1/5/18
to
W dniu piątek, 5 stycznia 2018 01:52:14 UTC+1 użytkownik Climate Hillbilly Davis napisał:
> Need to rename one file in batch to it's created date, i.e.,
> "latest.gho" > "createddate.gho"
>
> Format example: "01-04-2018.gho"
>

FOR %%i IN ("latest.gho") DO SET MYVAR=%%~ti

MYVAR is SET to date and time creation file like: "2018-01-05 07:23" (or in Your system date/time format settings)

Adam

Herbert Kleebauer

unread,
Jan 5, 2018, 5:09:00 AM1/5/18
to
On 05.01.2018 10:47, ada...@poczta.onet.pl wrote:

>> Need to rename one file in batch to it's created date, i.e.,
>> "latest.gho" > "createddate.gho"

> FOR %%i IN ("latest.gho") DO SET MYVAR=%%~ti
>
> MYVAR is SET to date and time creation file

I believe it is set to the last modified date, but
maybe that is what the OP wants, because the file
creation date it pretty meaningless (it changes every
time the file is copied).

Climate Hillbilly Davis

unread,
Jan 5, 2018, 5:23:14 AM1/5/18
to

On Fri, 5 Jan 2018 01:47:56 -0800 (PST), ada...@poczta.onet.pl says...

Need to rename one file in batch to it's created date, i.e.,
"latest.gho" > "createddate.gho"

Format example: "01-04-2018.gho"

Just one file, not a group.

I use this to rename it to current date: if exist D:\Latest.gho for /f
"tokens=1-5 delims=/ " %%d in ("%date%") do rename "D:\Latest.gho" %%e-%%
f-%%g_C.gho

Thanks.

> FOR %%i IN ("latest.gho") DO SET MYVAR=%%~ti

Thanks, but that seems to be incomplete. I'm running this from explorer,
not a command line.

I double click on the batch file and it changes the name of current ghost
file, then it runs ghost64.exe, naming it latest.gho.

This is how I had it set up before. Simple batch file.

=====

@echo on
cls
if exist D:\Latest.gho for /f "tokens=1-5 delims=/ " %%d in ("%date%") do
rename "D:\Latest.gho" %%e-%%f-%%g-rename_me.gho

move *.gho d:\ghost files\SSD1

H:\Programs\Ghost\Ghost32\Ghost64.exe -clone,mode=pcreate,src=1:1,dst="%%
e-%%f-%%g.gho", -sure

if exist D:\Latest.gho for /f "tokens=1-5 delims=/ " %%d in ("%date%") do
rename "D:\Latest.gho" %%e-%%f-%%g.gho

move *.gho d:\ghost files\SSD1

=====

I want the "old" file to be renamed to the its created or modified date,
not the system date.

rename latest.gho --> 01-05-2018.gho then move 01-05-2018.gho to different

then run ghost64.exe and have it name it latest then move it too.

I can easily do this manually, but it's a one click process now.

Climate Hillbilly Davis

unread,
Jan 5, 2018, 5:24:04 AM1/5/18
to

On Fri, 5 Jan 2018 11:08:57 +0100, Herbert Kleebauer says...
Yes, modified date is preferable. Haven't gotten anything to work yet.
Thanks.

JJ

unread,
Jan 6, 2018, 9:15:40 AM1/6/18
to
Use this. Pass the file as the first parameter.

@echo off
setlocal enabledelayedexpansion
if not exist %1 goto :eof
for %%F in ("%~1") do (
set d=%%~tF
ren "%%F" !d:~0,2!-!d:~3,2!-!d:~6,4!%%~xF
)

Climate Hillbilly Davis

unread,
Jan 6, 2018, 10:02:01 PM1/6/18
to

On Sat, 6 Jan 2018 21:15:09 +0700, JJ says...
Perfect... thank you very much!

I can't TELL you how many people on the net have written similar batch
files, but took about 500 more characters to do so, and STILL didn't work
right.

Thanks again.

Grant Taylor

unread,
Jan 6, 2018, 11:30:37 PM1/6/18
to
On 01/06/2018 08:01 PM, Climate Hillbilly Davis wrote:
> I can't TELL you how many people on the net have written similar batch
> files, but took about 500 more characters to do so, and STILL didn't work
> right.

If you're trying to save as many bytes as possible, try this:

@setlocal enabledelayedexpansion
@if exist %1 for %%F in ("%~1") do @(
@set d=%%~tF
@ren "%%F" !d:~0,2!-!d:~3,2!-!d:~6,4!%%~xF)

You save multiple characters by not using @ECHO OFF and instead putting
the @ character in front of commands that would otherwise show up on the
screen. - There's simply fewer commands than the bytes in the "@ECHO
OFF" string.

I also question the lack of (double) quotes around %1 in the if
statement. I guess it's fairly safe to not have them in the new name of
the ren(ame) command. Unless the extension has a space in it. }:-)



--
Grant. . . .
unix || die

Climate Hillbilly Davis

unread,
Jan 6, 2018, 11:45:15 PM1/6/18
to

On Sat, 6 Jan 2018 21:31:03 -0700, Grant Taylor says...

>
> On 01/06/2018 08:01 PM, Climate Hillbilly Davis wrote:
> > I can't TELL you how many people on the net have written similar batch
> > files, but took about 500 more characters to do so, and STILL didn't work
> > right.
>
> If you're trying to save as many bytes as possible, try this:

It's not a deal at all. The batch file is small to begin with.

I've got @echo off, spaces and cls's and moves and program starts, and
it's only 494 bytes.

> @setlocal enabledelayedexpansion
> @if exist %1 for %%F in ("%~1") do @(
> @set d=%%~tF
> @ren "%%F" !d:~0,2!-!d:~3,2!-!d:~6,4!%%~xF)

That worked fine too. Thanks.

> You save multiple characters by not using @ECHO OFF and instead putting
> the @ character in front of commands that would otherwise show up on the
> screen. - There's simply fewer commands than the bytes in the "@ECHO
> OFF" string.
>
> I also question the lack of (double) quotes around %1 in the if
> statement. I guess it's fairly safe to not have them in the new name of
> the ren(ame) command. Unless the extension has a space in it. }:-)

No space in the extension, so all is well.

I'm not sending a parameter, so I just changed the %1 to the name of the
file that I produce later in the batch file.

One question, though... what is the %~1? I also put the name of the file
there and it still works. What is the difference in %1 and %~1?

Thanks again.

Grant Taylor

unread,
Jan 6, 2018, 11:56:12 PM1/6/18
to
On 01/06/2018 09:45 PM, Climate Hillbilly Davis wrote:
> It's not a deal at all. The batch file is small to begin with.

*nod* That's what I figured. But based on your previous comment,
"…took about 500 more characters…" I thought you might be trying to be
as small as possible.

> I've got @echo off, spaces and cls's and moves and program starts,
> and it's only 494 bytes.

Based on my tests (that I've since removed) I think I was coming in at
~127 B compared to 158 B. So, a non-trivial percentage, …if you are
chasing every byte possible. ;-)

> That worked fine too. Thanks.

You're welcome.

> No space in the extension, so all is well.

*nod*

> I'm not sending a parameter, so I just changed the %1 to the name of
> the file that I produce later in the batch file.

You might be able to refactor things to remove the conditional and just
call the for loop directly.

> One question, though... what is the %~1? I also put the name of the file
> there and it still works. What is the difference in %1 and %~1?

Based on a quick skim of the output of "for /?", it looks like the tilde
without any letter between it and the variable name removes surrounding
quotes.

So I think the idea is to remove the quotes if they exist, and to
manually put them there. Thus avoiding a double quoting issue.

> Thanks again.

You're welcome.
0 new messages