Batch File to Append Date to file name
Saturday 24 Feb, 2007 - 21:49pm |
36 comments |
A DOS batch file (file with extension .bat) can be a very effective way to
automate a series of file operations. You can schedule the batch file to run at
a specific time, and automate, say, a series of operations such as copying,
moving, deleting or renaming files. You can create a batch file in any text
editor like notepad and just save the file with an extension of .bat. In
windows, if you open a DOS prompt, type edit and hit return a retro text
editor will open which you can also use.
The batch file code below will copy all microsoft excel files (.xls) from the
current folder into a folder called "import" and append the current
date in yyyymmdd format to the filename. Additionally the file extension is
changed from .xls to csv (I am not sure how readable the csv file will be to
other programs so let me know the result you get if you use it).
@Echo Off
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
@For %%a in ("*.xls") do copy %%a "import\%%~na_%All%.csv"
In this example the batch file needs to be saved in the same directory as the
Excel files.
There are many applications in using it. For example, sales people ftp their
expense reports to a folder, these are swept up at the end of a day, the date
appended to the filename, they are imported into a DB, the files are then moved
to an archive folder.
Posted in: Business
Tags: MSDOS | .bat | batch files | excel | csv
..
Monday 14 May, 2012 - 16:50pm
Just what I needed. Thanks alot.
Posted by: Anonymous
.. yyyy-MM-dd_hh.mm.ss
Thursday 03 May, 2012 - 10:30am
Tweaked version of the code to produce file names formatted as yyyy-MM-dd_hh.mm.ss
@Echo Off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C-%%B-%%A
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A.%%B.%%C
)
REM The following resolves the issue of single digit hours not being prefixed with a 0 - an underscore is added to end and if the 8th character is an underscore then the hour was single digit therefore add a zero to the start and then use the first 8 characters
set Allm=%Allm%_
if "%Allm:~7,1%"=="_" set Allm=0%Allm%
set Allm=%Allm:~0,8%
echo temp >> C:ackup\%All%_%Allm%.txt
Posted by: Anonymous
.. Finally! DOS yyyymmddhhmm that works!
Tuesday 27 Sep, 2011 - 23:38pm
Ok, yes, this is ugly, BUT it works, and it's not overly complicated. The problem was: appending a "yyyymmddhhmm" to a filename that really is 12 charaters, and does not drop the "0" before 8:00 AM. (or 3:00 PM).
Here we go:
@echo off
for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set y=%%k
for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set d=%%k%%i%%j
for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do set t=%%i%%j
set t=%t%_
if "%t:~3,1%"=="_" set t=0%t%
set t=%t:~0,4%
set "theFilename=%d%%t%"
echo %theFilename%
That's it. It works. Thank you, thank you very much...
TheSQLWhisperer
Posted by: Anonymous
.. cool technique
Tuesday 02 Aug, 2011 - 16:37pm
Thank you.
I like your programming style. easy to follow and to copy. Even though I am using powershell more and more, there is always some reason to come back to batchfiles.
Thanks again,
Dan
Posted by: Anonymous
.. Folder Copy accross Two drives
Tuesday 24 May, 2011 - 18:32pm
Great! This is the greatest thing I came accross on the net today. i have searched this information on the net for weeks now.
But I have a little problem. Please can someone help!
I needed to copy a set of folders from a network drive to a local hard disk drive on my computer, with the date appended. How can I tweak the batch program so that it performs this funtion?. Thank you very much
Posted by: Anonymous
..
Sunday 03 Apr, 2011 - 02:23am
Excellent! And very useful for me.
Thank you very much.
Posted by: Anonymous
.. Thanks so much
Wednesday 26 Jan, 2011 - 00:06am
Great little tool to add to the library!
Posted by: Anonymous
.. re: Batch File to Append Date to file name
Thursday 20 Jan, 2011 - 21:06pm
Excellent bit if work. Thank you all for your comments.
After I used this I also found another method which is a tad on the simple side:
@echo off
REM Create a log file with the current date and time in the filename
REM the ~4 in the Date skips the first four characters of the echoed date stamp and writes the remainder and so on
set LOG_FILE_NAME=Example_File_Name.%date:~4,2%%date:~7,2%%date:~10,4%.%time:~0,2%%time:~3,2%%time:~6,2%.txt
Echo This is much easier in UNIX > c: emp\%LOG_FILE_NAME%
:exit
Posted by: Anonymous
.. Awesome
Tuesday 19 Oct, 2010 - 17:30pm
Thanks for the tip, you save me hours of investigation and test.
Posted by: Anonymous
.. Extra help
Monday 11 Oct, 2010 - 10:03am
Hello, I am looking to add a right click option to the file explorer in Windows NT, to copy a file and add the date. For example,
before:
copy.txt
after:
copy.txt
copy_20101009.txt
I have no idea how the batch file should look although I know the very basics how to create one. Can anyone help?
Posted by: Anonymous
.. One last time, Apologies
Thursday 12 Aug, 2010 - 02:47am
Hi All,
It seems as though we all have the date sorted out but for those of you who would like the time appended to a filename, try this code:
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A%%B%%C
)
echo temp >> C:\%Allm%.txt
This code will now create a file called: 105545.txt
On the basis that it is 10:55:45 AM
@For /F "tokens=1,2,3,4 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set mSec=%%D
Set Allm=%%A%%B%%C%%D
)
echo temp >> C:\%Allm%.txt
The new file will now look like this: 10563222.txt
And just to show you some of what can be achieved, here is what i wrote:
@Echo Off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A.%%B.%%C
)
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
echo temp >> C:\%All%_%Allm%%AMPM%.txt
The Result: Thu-12-08-2010_11.02.46AM.txt
Enjoy!
Posted by: Anonymous
..
Thursday 12 Aug, 2010 - 02:36am
One more time for HTML...
Hi All,
It seems as though we all have the date sorted out but for those of you who would like the time appended to a filename, try this code:
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A%%B%%C
)
echo temp >> C:\%Allm%.txt
This code will now create a file called: 105545.txt
On the basis that it is 10:55:45 AM
If you wish to incorporate milliseconds into the filename use this:
@For /F "tokens=1,2,3,4 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set mSec=%%D
Set Allm=%%A%%B%%C%%D
)
echo temp >> C:\%Allm%.txt
The new file will now look like this: 10563222.txt
And just to show you some of what can be achieved, here is what i wrote:
@Echo Off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A.%%B.%%C
)
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
echo temp >> C:\%All%_%Allm%%AMPM%.txt
The Result: Thu-12-08-2010_11.02.46AM.txt
Enjoy!
Posted by: Anonymous
.. Time as a Filename
Thursday 12 Aug, 2010 - 02:24am
Hi All,
It seems as though we all have the date sorted out but for those of you who would like the time appended to a filename, try this code:
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A%%B%%C
)
echo temp >> C:\%Allm%.txt
This code will now create a file called: 105545.txt
On the basis that it is 10:55:45 AM
If you wish to incorporate milliseconds into the filename use this:
@For /F "tokens=1,2,3,4 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set mSec=%%D
Set Allm=%%A%%B%%C%%D
)
echo temp >> C:\%Allm%.txt
The new file will now look like this: 10563222.txt
And just to show you some of what can be achieved, here is what i wrote:
@Echo Off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A.%%B.%%C
)
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
echo temp >> C:\%All%_%Allm%%AMPM%.txt
The Result: Thu-12-08-2010_11.02.46AM.txt
Enjoy!
Posted by: Anonymous
.. good work
Monday 19 Jul, 2010 - 02:13am
i managed to modify this code slightly to achieve what i wanted. it is well written and was easy to understand for someone who doesnt do a lot of batch programming, all i had to do was cut and paste to the front of my existing code, reorder and rename some stuff, test and it works great. thanks
@echo off
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
copy b1*.csv bin1merge%All%.csv
copy b2*.csv bin2merge%All%.csv
copy b3*.csv bin3merge%All%.csv
copy r1*.csv rfid1merge%All%.csv
copy r2*.csv rfid2merge%All%.csv
copy r3*.csv rfid3merge%All%.csv
Posted by: Anonymous
.. No seconds
Wednesday 14 Jul, 2010 - 17:40pm
'time /t' does not display seconds (obviously, we would never want to know the seconds value. Thank you MS). If you try to use 'time', which does display seconds (and even tenths), the batch file will process the last line displayed, which is the 'Enter the new time' prompt. This is for WinXP Prof. (and likely Win2003 server). If anyone has a way to process the previous line, I'd be very interested to hear about it. Cheers.
Posted by: Anonymous
.. Copy across a Network share
Tuesday 27 Apr, 2010 - 06:49am
Hey I have tried -
@Echo Off
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
@For %%a in ("s:userackup*.xer") do copy %%a "z:Backup\%%~na_%All%.Xer"
And it wont seem to play the game. Does not give me an error or anything - just the files down seem to land on the Z: drive.
Can you see where I have stuffed the code up ?
Posted by: Anonymous
.. Works great
Monday 19 Apr, 2010 - 15:10pm
Thanks this works great but the only problem i have is that the file I need to rename is created at around 4:00am on a daily bases detialing the previous days business so i need to rename it with yesterdays date
for example
file created
cbh.xls
created on monday 19th April but needs to be renamed
cbh-18-04-2010.xls
so is there a way that i can get todays date and then -1 from it then append that to the file
thanks
Andy
Posted by: Anonymous
..
Thursday 01 Apr, 2010 - 20:45pm
time /t doesn't show seconds (on my copy of Windows anyway).
I use echo %time% instead.
Posted by: Anonymous
.. output seconds
Wednesday 31 Mar, 2010 - 11:00am
Hi,
I like the script.
I am having a problem adding the Seconds (time)
I get the following lbicc_20100329_1458.txt
The below doesn't seem to work???
@Echo Off
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
@For /F "tokens=1,2,3 delims=: " %%A in ('Time /t') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A%%B%%C
)
@For %%a in ("*.txt") do copy %%a "import\%%~na_%All%_%Allm%.txt"
Thanks
Cliff
Posted by: Anonymous
..
Sunday 14 Mar, 2010 - 18:03pm
What a marvelous thing you have posted, I very much appreciated that you have taken the time and effort to do this. With a little tweaking, I was able to make this do exactly what I wanted! Thank you very, very much!
Doug R., Calgary, Canada
Posted by: Anonymous
..
Monday 08 Mar, 2010 - 07:24am
Hey I have been using this to append data & time to a couple of very important files and has saved me on a couple of occassions. I was using it in XP and worked great. Now I have upgraded to Win 7 and now not working so well. Where I used to get the output of 2009_11_29_0647pm<filename> I now get _2010_02_1900<filename>. So has anyone has any issue with running this on a Win 7 PC & how can it be fixed?
Posted by: Anonymous
.. Works perfectly thanks
Saturday 28 Nov, 2009 - 00:00am
The comment from Monday 07 Sep, 2009 - 12:55pm worked great on Windows 2000 even though he said it was for Windows 2003.
I had to add quotes around the Copy parameter aa to handle Long File Names.
Thank you!
Posted by: Anonymous
.. For windows 2003 Server
Monday 07 Sep, 2009 - 12:55pm
For windows 2003 server you need a little bit of a code change, try this
@echo off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Date=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
Posted by: Anonymous
.. very useful
Friday 26 Jun, 2009 - 11:29am
That was very simple to understand and its helped me a lot
thanks you kay
Posted by: Anonymous
..
Monday 18 May, 2009 - 19:47pm
Hi, Excelent, it was very useful for me.
Posted by: Anonymous
.. Better Time display
Tuesday 17 Feb, 2009 - 23:19pm
Thanks for this code! I've always wondered how to use the For/In/Do functions in batch files and your simple but highly functional example really made it easy to figure out.
I liked it so much here's my additions to it in case anyone else can benefit from it:
DOS has built-in variables for %Date% and %Time% and if you do an command, you get the exact same output as . On the other hand, if you use you might get but using you get which is the 24 Hour time all the way down to millisecond granularity which is perfect for use in file names where multiple files get created at multiple times and the filesystem sorts on the name by default.
Here's my version that takes the Date and Time and puts them into a FileName variable containing the full path to the file:
For /F "tokens=2,3,4 delims=/ " %%A in ('Echo %Date%') do Set FileDate=%%C-%%A-%%B
For /F "tokens=1,2,3,4 delims=:." %%A in ('Echo %Time%') do Set FileTime=%%A-%%B-%%C-%%D
Set FileName="C:TestFile_%FileDate%_%FileTime%.txt"
If you change your own pre-existing code, don't miss that both the "tokens" and "delims" parts are different to accommodate the differences in the time formats and to put it into HH-MM-SS-mm format. I've also changed the Date order into YYYY-MM-DD format.
Posted by: Anonymous
.. To get year
Wednesday 14 Jan, 2009 - 05:27am
Thanks... here is mod I did to rename and get year on end
@Echo Off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set Year=%%D
Set All=%%A%%B%%C%%D
)
@For %%a in ("*.jpg") do rename %%a "%%~na_%All%.jpg"
I added the D token so its now day month date year on add on date to file... thanks for great code I looked a long time...
thanks again
Vistronic
Posted by: Anonymous
..
Wednesday 22 Oct, 2008 - 18:56pm
THANK YOU very much for this information!! It has helped me today.
Posted by: Anonymous
.. thank you
Friday 17 Oct, 2008 - 22:42pm
Thank you for your time in developing this. By making just a few small adjustments I was able to adapt the script for my own needs. You have saved me much time and I am extremely grateful.
THANK YOU!
Posted by: Anonymous
..
Tuesday 27 May, 2008 - 19:01pm
Perfect! Thank you very much.
Posted by: Anonymous
.. Use the Time Function
Thursday 22 May, 2008 - 22:18pm
The code below will give you the result though, admittedly,
it's not very elegant
@Echo Off
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
@For /F "tokens=1,2,3 delims=: " %%A in ('Time /t') do @(
Set Hour=%%A
Set Min=%%B
Set Allm=%%A%%B
)
@For %%a in ("*.xls") do copy %%a "import\%%~na_%All%_%Allm%.csv"
Posted by: ZorbaTheGeek
..
Thursday 22 May, 2008 - 19:13pm
Hello. I really like that small batch file. Can you tell me how to get the hour and minute to appear in the file name as well as everything else you listed? I tried playing around with the Set time command but my batch file knowledge is almost nil. Thanks.
Posted by: Anonymous
..
Tuesday 18 Dec, 2007 - 02:56am
Ok great, thanks for the update. Glad you resolved it.
Posted by: ZorbaTheGeek
..
Monday 17 Dec, 2007 - 16:56pm
In response to my problem below, I figured out how to tweak it - tokens needed to be changed to 2,3,4 and reordered the variables. This should give the expected yyyymmdd
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
Set All=%%C%%A%%B
)
Posted by: Anonymous
..
Monday 17 Dec, 2007 - 16:25pm
I need this exact functionality but the results of the batch are not as expected/stated in your post...
When i run it today Monday, 12/17/07 the part it appends is 1712Mon. Run from Server 2003. Any ideas?
Posted by: Anonymous
.. great
Wednesday 05 Dec, 2007 - 15:07pm
very useful bit of code....Thanks!
Posted by: Anonymous