Batch: adding a time stamp to file names

I'd like to add time stamp to file names like this:

file1.txt -> file1%timestamp%.txt
file2.txt -> file2%timestamp%.txt

How do I do it so that timestamp doesn't come after .txt extension?

Thanks!
gromulAsked:
Who is Participating?
 
Lee W, MVPConnect With a Mentor Technology and Business Process AdvisorCommented:
That can get a little trickier - ASSUMING the file names DO NOT have more than one period in them - to seperate the extensions, then use this:

FOR /F "tokens=1,2 delims=." %%a in ('dir /b /a-d') do @ren %%a.%%b %%a%date:~-4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%%time:~6,2%.%%b

If used OUTSIDE of a batch file, use SINGLE percents on the %%a references:

FOR /F "tokens=1,2 delims=." %a in ('dir /b /a-d') do @ren %a.%b %a%date:~-4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%%time:~6,2%.%b


What I mean by one period:

file1.txt
file2.txt
file3.txt
These would be ok,

this.is.file.1.txt
another.file.txt
file3.txt
This would NOT work.
0
 
gromulAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.