Link to home
Get AccessLog in
Avatar of Gary Antonellis
Gary AntonellisFlag for United States of America

asked on

How do I create filename with date/time using BAT script

I need to rename a file with the date/time.  The following script is failing when the time is between 1:00 and 9:59 because the time is one digit and the string is shifting left on character.

ren Filename.log Filename%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%.log
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of Gary Antonellis

ASKER

THanks!
Avatar of Bill Prew
Bill Prew

Glad that helped, thanks.

~bp
Please note this all breaks when you schedule the job to run as a different user which may have a different date format etc. so be careful.  I prefer this method:

https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html