This DOS Batch file will use today's date to create a subdirectory. So, if today is 10/25/2004, then you'll get a subdirectory path named 2004\10\25.
This shows the ability of DOS to parse strings, and the use of system variables, namely the %date% var. It uses the optional parsing parameters, :~#,# where the first number is where to start (the first character is 0) and the second number is how many characters to take. It took me a couple hours to figure all this out the other day, I hope I can save you the pain I suffered.
@echo off
cls
echo This will create a subdirectory with today's date as the name.
echo The date looks like this: %date%
echo We'll use %%date%%
echo and take the last 4 characters (the year) using :~-4,4
echo The -4 is "4 in from the right" and 4 is the number of characters to take.
echo ---
echo The command looks like this in the batch file:
echo md %%date:~-4,4%%\%%date:~-10,2%%\%%date:~-7,2%%
echo ---
echo but to DOS it looks like this:
echo md %date:~-4,4%\%date:~-10,2%\%date:~-7,2%
echo ---
pause
@echo on
md %date:~-4,4%\%date:~-10,2%\%date:~-7,2%
Updated 7/18/2007:
Here is how to do an action and log the output to a daily log file. I find this useful for my batch file that erases blacklisted emails from my Exchnage server. For this example, I'll just show you using the DIR command. Any dos program or batch file that outputs text to the console can be redirected to a file.
dir >log_%date:~-4,4%.%date:~-10,2%.%date:~-7,2%.txt
This is easy... the DIR command outputs the contents of the directory as a list in plain text. We are redirecting the output of this operation to a file. The filename will start with the word log_ and look like this: log_2007.07.18.txt.
But what if you want the process to run every 30 minutes, and add lines to the log file all day, rather than replace it? just double up the > like this:
dir >>log_%date:~-4,4%.%date:~-10,2%.%date:~-7,2%.txt
Now the dir command will add lines to the same file rather than overwriting the previous run.
other possibilities:
pkzip *.jpg images_%date:~-4,4%.%date:~-10,2%.%date:~-7,2%.zip -m
del *.tmp >deleted_%date:~-4,4%.%date:~-10,2%.%date:~-7,2%.log
md archive_%date:~-4,4%\%date:~-10,2%\%date:~-7,2%
I hope this helps!



55 comments:
For those of us that are unix/linux savvy ;-)
mkdir `date +%Y/%m/%d`
Thanks, B&R! That was very helpful for me.
got your page from google with echo date dos it did exactly what I needed. Thanks for the help.
*wishes he was allowed to use what he wants to at work*
Works perfectly , many thanks. Ivan, Slovakia
Very nicely done!
.. and here I was thinking I had a job in my hands. You certainly saved me the pain.. good one, thank you!
Thanks, for this... It was helpful, but heres the problem I have, I don't need the current month, but the last month (and last year incase last month was December)
How would I get this??
Maybe you can email me the answer?
Sorry, I don't think DOS is that smart. It has no ability to do subtraction. If you say "echo 2-4", it gives "2-4".
Soo am i skrewed?
There might be a way using if's to convert the months, but you'd have to hardcode for the years.
DOS is not really a programming language. If you want to do something like that, you should make an executable that does it for you.
echo off
setlocal
echo =============================
echo Find last month
echo =============================
set /p month=Enter this month (as a number):
if %month% lss 1 goto error
if %month% gtr 12 goto error
set /a month=%month%-1
if %month% equ 0 set month=12
if %month% lss 10 set month=0%month%
echo Last month was %month%.
goto end
:error
echo And just what calendar do YOU use? (1-12, please)
:end
endlocal
WOOT ... MAN, THATS WHAT I NEED !
THX 'n SMILE
I love you man! I've been struggling with this since yesterday - you da man!
I was looking for a way to manipulate the date format in a batch file and this is just what I needed. It works great.
Thanks
Gentlemen:
This is outstanding information. I cannot thank you enough for making it available. (A quick Google search brought me right to it.) It is exactly what I've been looking for and has saved me an enormous amount of time.
Thanks again.
- JJM
Chicago, IL
Can this be used with the RENAME command?
RENAME file named "DATA.txt" to "DATA-YYMMDD-HHMMSS.txt"
Thanks for the valuable information. You can ignore my previous comments (5/08/2006 6:33 AM)as I have since figured out how to tweak this for my use using the RENAME command and add the time too.
ren FILENAME1.txt DATA-%date:~-4,4%%date:~-10,2%%date:~-7,2%-%time:~-11,2%%time:~-8,2%%time:~-5,2%.txt
sweetness !!
replace \ with - and have a single level dir with todays date. perfect.
thanks++
thanks so much!!
Brilliant. I'd checked out a half dozen search results on naming files/folders with the date via a .bat and none of the others worked properly.
I have also found this useful! I have bat files that run my web stats generator (Awstats) -- But I want them to generate for LAST month because last month is complete. Your info is what I needed! Thanks a lot!
www.GraceBaptist.ws
I was so happy to find this! But I'm still having difficulty getting mine to work. I want to rename a file with the previous month included in the filename. I'm able to rename it with the current month using this:
ren ABC.pdf "ABC %date:~4,2%-%date:~12%.pdf"
Which produces: ABC 10-06.pdf
What do I need to change to bring in the previous month? Thanks!
Using the code posted on 6/22/2005 as a base:
@echo off
SET MONTH=%DATE:~4,2%
SET YEAR=%DATE:~10,4%
SET /a MONTH=%MONTH%-1
if %MONTH% equ 0 SET /a YEAR=%YEAR%-1
if %MONTH% equ 0 SET MONTH=12
if %MONTH% lss 10 SET MONTH=0%MONTH%
SET FILENAME=ABC %MONTH%-%YEAR:~2,2%
REN ABC.txt "%FILENAME%.txt"
Thank-you guys, the code for creating a filename was so easy with this page.
I used :
echo this is my file > %date:~-4,4%%date:~-10,2%%date:~-7,2%.txt to put the output of something into a file that was named using the date.
It is so simple - unlike all the complicated ways I have been seeing on other sites!
Here's another tidbit I learned the hard way:
If you want to include the hour in the file or directory name, it can get tricky for the hours before 10 AM. That's because %TIME% doesn't zero-pad the hour. (It does, however, pad the hour with a space character for hours before 10AM.)
Anyway, the full approach is as follows:
rem Unfortunately, the TIME variable doesn't 0-pad the hour.
rem So do it ourselves.
set UNPADDEDHOURWITHSPACE=%TIME:~0,2%
set /a UNPADDEDHOUR=%UNPADDEDHOURWITHSPACE%
set EXTRAZERO=0%UNPADDEDHOUR%
set HOUR=%EXTRAZERO:~-2%
@echo Creating a directory with today's date
set datename=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%HOUR%-%TIME:~3,2%
mkdir %datename%
Note use of "set /a" to get rid of the leading space in UNPADDEDHOURWITHSPACE!
many thanks for this, saved valuable time. I had to make a small tweak due to living in UK (%date:~-4,4%\%date:~-7,2%\%date:~-10,2%), but alls good.
I can't get this to work. my server is windows 2000. Do you know if it works on windows 2000? do you have a list os OS that supports the date edit? Thank you
do you know is this is working on windows 2000?
It is not working for me so far =/
Thanks
It's really fantastic, thank you very much
I can confirm that the date and time batch for mkdir works with Vista Enterprise.
You can also use it with ren to rename directories.
Nice tidbit. Saved me some trouble.
This is brilliant - many thanks.
Many thanks , this really helped me out
Saved me a lot of trouble
Excellent - /a saved the day!
Here's another way to solve the no zero padding hour problem with %TIME%:
SET current_time=%time: =0%
This replaces the spaces with zeros. You can then parse this variable to get the zero padded hour, e.g:
%current_time:~0,2%
your code helped me alot to set up a heavy process....... thank a bunch....
THX & Smile Chris
using SET paddedtime=%time: =0% to zero pad is an absolutely brilliant shortcut
Shame you can't combine this with ~0,2 in one command. But this has still saved me lines of code,
Hi, I am trying to save today date in a variable and create a directory using that variable (I do this because my batch file will run 18 hours and I need to save today date somewhere,)
I used this command, but it is not working, any help please?
SET TODAY = %date:~-6,3%%date:~-9,2%
mkdir %TODAY%
You did it wrong.
Just look what TODAY is set to by typing
SET TODAY
you get 5/22/
Sometimes you have to do your own debugging.
thank you for suggestion, but this is exactly what I need to do, so I think there was something wrong with my Dos version since changing the command to the following works, by the way, this is amazing website, I learned a lot from you.
set datename=%DATE:~-6,3%%DATE:~-9,2%
will create Feb18 directory.
Apparently MS's latest XP patch has just broken this formatting, as far as I can make out the initial tilde is not parsed correctly and DATE or TIME commands must be escaped in percentages.
to boil it down in case someone missed it - very neat and easy filename with formatted date and time
set current_date=%date:/=-%
creates new variable and replaces forward slashes / (in %date%) with hyphen -
set current_time=%time: =0%
new variable created with spaces replaced by zeros 0
set current_time=%current_time::=-%
process current_time again to change colons : into hyphens -
set myfilestring=log_%current_date:~4,10%_%current_time:~0,8%.any
if typing echo %date% on your machine shows this format "Fri 01/02/2008"
and echo %time% shows something like "9:02:06.73"
then after the four SET commands above are run, echo %myfilestring% will show "log_01-02-2008_09-02-06.any"
if you needed the decimal point in the time you could process %current_time% again using SET current_time=%current_time:.=-% to change decimal point . to a hyphen -
phew, the trailing space in the time fix saved me heaps of time - THANKS!
Thanks for all the information. I will book mark this page for future reference.
Excellent, just excellent
This has certainly come in handy! First off, I was trying to create a text file with a list of Excel document paths within directories, along with their last-modified dates. The only way I could figure how to do this was to append to a text file, which would mean I would have to delete the file each time. However, with using a date/time-stamp for the file name I no longer need to worry about this! I only had to adjust the positioning for the date and time (although, padding was not required in my case):
@echo off
:: May or may not need to pad month and hour, so better to be safe.
:: You may need to change the positioning for the PADMTH or PADHOUR,
:: depending on your system settings. Just echo.%date% & echo.%time%
set PADMTH=0%DATE:~5,2%
set MTH=%PADMTH:~-2%
set PADHOUR=0%TIME:~0,2%
set HOUR=%PADHOUR:~-2%
set datename=%DATE:~0,4%%MTH%%DATE:~8,2%_%HOUR%%TIME:~3,2%%TIME:~6,2%
for /r %%F in (*.xl*) do (echo.%%~ftF >> Search_results_%datename%.txt)
I don't really know DOS all that well so if someone sees any problems/inefficiencies please post!
Thanks !
A priceless public service !
What I need to do is to parse %ComputerName% to create another variable to be used in a batch process. I need various parts of the variable but not the whole thing. The variable result would be in the fornmat xxx-xxx-xxx and I would need the first 3 characters at one point and the second 3 characters at another. I would certainly appreciate it if one of you caouls put me on the right track for this.
Everything you needed was already here but this will solve your problem.
Note that my computer name does not conform to yours, so I created a variable named "Computer"
@echo off
set Computer=123-456-789
echo %Computer%
set A=%Computer:~0,3%
set B=%Computer:~4,3%
set C=%Computer:~8,3%
echo %A% %B% %C%
pause
SET /a MONTH=%MONTH%-1
returns the error
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
This was all very useful. Thanks to all.
I need yesterday's date for an automated daily function too and elected for a quick cheat solution!
Rather than calculating month-1, adjusting for January, calculating day-1 and wondering how many days previous month had (30,31,28 ...)
I calculate the value I will need tomorrow from today's date and store it in a variable , and so each day I use the variable created the previous day and easily prepare one for tomorrow from today's date.
For the invalid number issue, try this...
set month=%month:0=%
...then try SET /a MONTH=%MONTH%-1
It doesn't like the 0, so this will remove it.
Thanks to evryone on this post...this has been a great help!
toolman's fix for the invalid number issue didn't help me b/c it changed month = 10 into month = 1
here is my script:
@echo off
:: find the year
set year=%DATE:~10,4%
:: find the current month
set /a m1=%DATE:~4,1%
IF %m1% ==0 (set month=%DATE:~5,1%) ELSE (set month=%DATE:~4,2%)
echo current month
echo ------------------
echo month = %month%
echo year = %year%
echo.
:: find the previous month and year
IF %month% ==1 (set /a year=%year%-1)
IF %month% ==1 (set /a month=12) ELSE (set /a month=%month%-1)
echo previous month
echo ------------------
echo month = %month%
echo year = %year%
echo.
IF %month% LSS 10 set month=0%month%
echo previous padded
echo ------------------
echo month = %month%
echo year = %year%
echo.
-----------
there are a bunch of comments and unnecessary output, but it may help someone...
Thanks everyone for all of you tips!
How would you change to this newly created subdirectory? I would like to change to this subdirectory so that I can copy files into this newly created subdirectory. Thanks for your help.
Geez, there are at least 10 examples here but once you construct a date string you can use it in any DOS command.
echo %MyDirectoryname%
md %MyDirectoryname%
copy * %MyDirectoryname%
Notepad.exe %MyDirectoryname%\ABC.TXT
Winzip %MyDirectoryname%
etc.
Thanks for your post!
I was looking something like this to use in dos systems to do automatic backups with date and timestamp. If copy/paste the following line in dos console, it will show the date, time in my desired format and also the time in standard format in order to make a comparison:
echo %date:~-4,4%.%date:~-10,2%.%date:~-7,2% %time:~-0,
2%%time:~3,-6%%time:~6,-3%%time:~9,3% %time%
md 2009.15.04 9083316 9:08:33,16
Post a Comment