• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

Rename a file using a batch script to have date and time

I am trying to use a batch script to rename a file to have date and time. So far I am able to add the date but cannot figure out how to get the time also. Below is what I have so far that works.


for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "pa.txt" pa%%g%%e%%f.txt
0
edensandavant
Asked:
edensandavant
  • 3
1 Solution
 
Steve KnightIT ConsultancyCommented:
You could add time in the same way:

for /f "tokens=1-9 delims=/: " %%d in ("%date% %time%") do echo rename "pa.txt" pa%%g%%e%%f%%h%%i.txt

I think, off hand!

I tend to use

set datetime=%date:/=%%time::=% for a quick and dirty date/time

Steve
0
 
Steve KnightIT ConsultancyCommented:
Remove the "echo" there once happy with the filename of course...
0
 
edensandavantAuthor Commented:
That worked perfectly. Thanks.
0
 
Steve KnightIT ConsultancyCommented:
No problem... just splits up at the : in the time aswell as the original / for dates.

Steve
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now