Thursday, October 2, 2008

Rename file and append date or time stamp using batch script

I found an even easier way to do this, but have left the original post below. You can use a combination of environmental variables and substring commands to do the rename right inline. All you have to do is add the following, depending on what you want to include:

%date:~-4,4% - Year
%date:~-7,2% - Month
%date:~-10,2% - Day
%time:~-11,2% - Hours
%time:~-8,2% - Minutes
%time:~-5,2% - Seconds
%time:~-2,2% - Milliseconds

If you want to datestamp a file with YYYYMMDD, you can use the command

ren file.txt file-%date:~-4,4%%date:~-7,2%%date:~-10,2%

If you want to timestamp a file with HHMMSS (no milliseconds), you can use

ren file.txt file-%time:~-11,2%%time:~-8,2%%time:~-5,2%

Then you can also use any combination of those to create a date and time stamp if you'd like.

To see where I found this information, check it out here

Original Post

You can use a simple batch script to rename a file and add a datestamp to it. The code is below. You can modify the filename to be the path and name of the file you want. You can change "copy" to "ren" if you want to just rename the original. Leave it as "copy" if you want to create a copy of the file. You can also change the path where you add the date to the name too if you want to create a copy in a different location. Here's the example code:
for /f "tokens=2-4* delims=/ " %%a in ('DATE /T') do set THEDATE=%%c%%a%%b

copy /Y/V C:\filename.txt C:\filename%THEDATE%.txt
In the code above, THEDATE is defined by variables a, b, and c. Those stand for the day, month, and year.
a = month
b = day
c = year
You can rearrange them as needed, which I already did for my purposes. The code above with show the date as yyyymmdd. You'll want to make sure the lines of code all appear on the same line. I could've posted an image with everything looking nice, but this way you can copy and paste that code which I know most will be grateful for.
The original post that helped me can be found here. There is also more code in that post that includes using the time rather than just the date.


Sidinei Mancini said...

Thank You For The Post!

Brice2nice said...


Nilesh said...

I have below requirement to append date in MMDDYYYY format after second ~ (delimiter) to all my excel files in particluar directory

Original File Name-
Expected output-

Can you please suggest how to do this whitout copying/overwriting & just with rename..
Thanks in Advance

rslygh said...


You can use the rename in the top of the article, and just change it around to come out in MMDDYYYY format. It would be:

ren Canada_report~D~.xls Canada_report~D~%date:~-7,2%%date:~-10,2%%date:~-4,4%.xls

As far as looping through all your files in a folder using that command, there are sources out there for that. One is

roadr13 said...

After searching for a few hours I stumbled upon your LIFE SAVING blog!

You rock! It is very simple to use and it works beautifully.

Thank you for sharing with the rest of the world.

Anonymous said...

Thanks. Excellent solution for my problem.

Anonymous said...


How about I want to add the current week number. Thanks.

rslygh said...

Plenty of results for getting the week number out there. and are a couple that show up on the first page of Google search