Computing.Net > Forums > Programming > Batch to rename file to incl military time

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch to rename file to incl military time

Reply to Message Icon

Name: asaet231
Date: November 2, 2009 at 13:24:33 Pacific
OS: Windows XP
Subcategory: Batch
Tags: batch, rename
Comment:

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)
)
)



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: November 3, 2009 at 05:31:02 Pacific
Reply:

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.)


0

Response Number 2
Name: asaet231
Date: November 3, 2009 at 05:55:22 Pacific
Reply:

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.


0

Response Number 3
Name: klint
Date: November 3, 2009 at 06:39:19 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Accessing to a network how do you a poll a msgbo...


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Batch to rename file to incl military time

Batch Script To Rename File www.computing.net/answers/programming/batch-script-to-rename-file/16798.html

batch to rename based on fileconten www.computing.net/answers/programming/batch-to-rename-based-on-fileconten/17419.html

Editing a txt file, renaming files www.computing.net/answers/programming/editing-a-txt-file-renaming-files/16242.html