Avatar of Gary Antonellis
Gary Antonellis
Flag 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
Microsoft DOSWindows XP

Avatar of undefined
Last Comment
Steve Knight

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Bill Prew

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Gary Antonellis

ASKER
THanks!
Bill Prew

Glad that helped, thanks.

~bp
Steve Knight

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck