Batch to rename file to incl military time

November 2, 2009 at 13:24:33
Specs: Windows XP
Trying to rename multiple existing files to append their create date/time (in military time format) to the filename.

Current code:
FOR %%V IN (SELT*.EXT) DO (
FOR /F "tokens=1-6 delims=/: " %%J IN ("%%~tV") DO (
SET CONV=%%M%%N
IF %%O == 'PM' IF %%M%%N LSS '1200' DO (
SET /A CONV=%%M%%N+1200
ECHO CONV=%CONV%
PAUSE
)
SET CONV=%CONV%
SET DAY=%%L%%J%%K
IF EXIST BANNER_%DAY%_%CONV%.EXT (ECHO Cannot rename %%V) ELSE (Copy "%%V" BANNER_%DAY%_%CONV%.EXT)
)
)


See More: Batch to rename file to incl military time

Report •


#1
November 3, 2009 at 05:31:02
What's wrong with your current program? (It may be obvious if I try it, but I haven't tried it, as it would be easier if you told us what's wrong with it yourself as you know it better than any of us, since you wrote it.)

Report •

#2
November 3, 2009 at 05:55:22
My bad.

I start with 3 SELT*.EXT files, create times are 10a, 12p and 2p. At the end of the batch run, I only get one copy of the 10a file and its name is BANNER_%L%J%K_.EXT.


Report •

#3
November 3, 2009 at 06:39:19
Hi again, here's a few comments on your code that I hope will help.

IF %%O == 'PM' IF %%M%%N LSS '1200' DO (

Leave out the quotes and the DO:

IF %%O == PM IF %%M%%N LSS 1200 (

The command processor compares literally: if the left-hand side has no quotes, the right-hand side mustn't have quotes either. Unless you want to test if a variable's value contains the quotes.

The DO is not part of an IF statement, and the only reason you didn't get a syntax error reported was that the IF condition was never met.

SET CONV=%CONV%

In a stand-alone statement, the above is totally pointless: it assigns to the variable the value that it already has. But in your case, in a statement inside a FOR loop, it is worse than pointless. It assigns to CONV the value that CONV had before it entered the FOR loop. (Type SET /? and read the bit about delayed variable expansion for more information.)

Put this line:
SETLOCAL ENABLEDELAYEDEXPANSION
near the top of your file, and where you have %CONV%, use !CONV! instead.

Also, be aware that this logic only works in the US and very few other countries where the filestamp is formatted that way.


Report •

Related Solutions


Ask Question