Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch File: File file with date in name, and rename. 1

Status
Not open for further replies.

ChrisCalvert

Technical User
Joined
Mar 18, 2002
Messages
231
Location
US
I see a few subjects in here about adding a date to a file name, but I need to see if anyone can help me with a little modification to this. I need to locate a file named SMFyymmdd and rename it. The yy is 2digit year and mmdd is the two digit month and day. The code below is what I was looking to modify, but if someone has entirely new code, that is fine too. I just need to rename these files (adding a .txt extension). All I really need it a way to strip off the first two digits of the date, or some way not to ever get them at all.

Thanks in advance for any help.


-----------------------------------------------
date /t returns "Wed 06/04/2003"
time /t returns "15:29"

for /f "tokens=2 delims= " %%a in ('date /t') do set temp=%%a

This strips off "Wed" leaving you with "06/04/2003"

for /f "tokens=1-3 delims=/" %%a in ("%temp%") do (set month=%%a& set day=%%b& set year=%%c)

*****I know I need to change this somehow, as I only
want the 03 and not the 2003***********

Now we have 06 & 04 & 2003 in the environment

for /f "tokens=1-2 delims=:" %%a in ('time /t') do (set hour=%%a& set min=%%b)

and time is split into 15 & 29

ren test.txt %month%-%day%-%year%.txt

****and here, I suppose I just reverse this command.*****
 
Chris,

A suggestion, abeit one that will need a short rewrite in January 2010:

Add an extra line to your batch file after the month, day and year have been extracted:

for /f "tokens=2 delims=0" %%a in ("%year%") do set year=%%a

This will extract the last digit of the year, without the leading zero, so year will have the value 3, at the moment.

You can then use the following rename command instead:

ren test.txt %month%-%day%-0%year%.txt

If anybody has any better suggestions I am welcome to them.

Just another point though: The Temp environment variable is used by the operating system to determine where it should place its temporary files. I would certainly use a different name instead of temp, as any system would have a problem saving files to a directory named 17/07/2003 (in my case), or at the end of your batch file use
SET temp=%tmp% to set it back to what it was before.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top