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:
C:
@ECHO OFF
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
:end
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.
10 comments:
Thank You For The Post!
Thanx
Hi,
I have below requirement to append date in MMDDYYYY format after second ~ (delimiter) to all my excel files in particluar directory
Original File Name-
Canada_report~D~.xls
Expected output-
Canada_report~D~05022013.xls
Can you please suggest how to do this whitout copying/overwriting & just with rename..
Thanks in Advance
Nilesh,
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 http://stackoverflow.com/questions/138497/batch-scripting-iterating-over-files-in-a-directory
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.
Thanks. Excellent solution for my problem.
Hi,
How about I want to add the current week number. Thanks.
Plenty of results for getting the week number out there. http://www.robvanderwoude.com/sourcecode.php?src=week_nt and http://www.netikka.net/tsneti/info/tscmd149.htm are a couple that show up on the first page of Google search
I know this was posted a while back. But it is so easy to understand, thank you. I am a real novice at this. I'm looking to rename a file by appending the LAST MODIFIED date in a bat or vbs file. thank you.
You're welcome. Glad it helped
Post a Comment