Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2875
  • Last Modified:

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!
0
gromul
Asked:
gromul
1 Solution
 
Lee W, MVPTechnology 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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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