How to create a folder with name as current date in batch (.bat) files
Solution 1
Try this (an equivalent of bash backquotes):
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir %datestr%
For further information, see http://ss64.com/nt/for_cmd.html
Solution 2
mkdir %date:~-4,4%%date:~-10,2%%date:~7,2%
Solution 3
Quick and dirty: If you can live with the date being UTC instead of local, you can use:
for /f "skip=1" %%d in ('wmic os get localdatetime') do if not defined mydate set mydate=%%d
md %mydate:~0,8%
Works in all locales. Only on XP and higher, though.
Solution 4
You need to get rid of the '/' characters in the date before you can use it in mkdir like this:
setlocal enableextensions
set name=%DATE:/=_%
mkdir %name%
Solution 5
If you want mm-dd-yyyy
format you can use:
mkdir %date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%
Related videos on Youtube
Ramesh Soni
I am fun loving guy and a techie working as a engineer in a software development firm. My hobbies and interests are all around my profession. These includes programming, computer games, technology, internet..
Updated on February 24, 2021Comments
-
Ramesh Soni about 3 years
I don't know much about windows .bat file syntax. My simple requirement is to create a folder at a specific location with name as current date. I tried searching this on google but didn't get any good option. Is there any way to do this?
-
feeela about 4 yearsDoes this answer your question? How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
-
-
Ramesh Soni about 13 years@Blorgbeard this is creating folder with name T2011 where 2011 is year. I am using WindowsXp but not sure why there is not month and day in it.
-
Ramesh Soni about 13 yearssurprisingly this is creating folder with name
T2011
. There is not month and day in it. -
Blorgbeard about 13 years@Ramesh, what does
echo %date%
output? It will depend on your regional settings - mine says "2011-03-30" -
Blorgbeard about 13 yearsHeavily dependent on regional settings
-
Ramesh Soni about 13 years@Blorgbeard even the folder name is created as T2011. I am using standard US English culture. Is there anything I am missing?
-
Blorgbeard about 13 years@Ramesh, does
echo %date%
outputT2011
and nothing else? -
Simon G. about 13 yearsIf you
echo %DATE%
you should see the current date. The format depends on the international locale settings. If you want this bat file to run anywhere, you'll need to do something complicated.I assumed it was a day, month, and year separated by a / character. Since you have a T in the result, I guess it is not. What is it? -
Simon G. about 13 yearsHaving thought about it for a few minutes and googled a bit, this method using JavaScript should work wherever you are in the world:
echo var D = new Date() > tmp.js
echo D = (D.getFullYear()*100+D.getMonth()+1)*100+D.getDate() >> tmp.js
echo WScript.Echo( 'set YYYYMMDD='+D ) >> tmp.js
echo @echo off > tmp.bat
cscript //nologo tmp.js >> tmp.bat
call tmp.bat
mkdir %YYYYMMDD%
-
Simon G. about 13 yearswell I can't get that to format properly - I will add a new answer
-
Ramesh Soni about 13 years@Blorgbeard yes just T2011 and nothing else.
-
Blorgbeard about 13 years@Ramesh OK, then I am stumped, sorry :) Simon's answer looks promising..
-
Adriano Repetti about 11 yearsTo make it more easy:
mkdir %date:/=%
-
eckes over 10 years@Adriano: impressive. Could you elaborate a bit on the
:/=
thing? Opened a question for this: stackoverflow.com/q/21032288/520162 -
Bernhard Hofmann over 10 yearsI'd +1 that if you used %TEMP% or %tmp% as the path and if you deleted the temporary files. :)
-
mikequentel over 9 yearsExcellent answer. To dynamically keep making new directories with date and time I used
for /f "skip=1" %%d in ('wmic os get localdatetime') do set mydate=%%d
followed bymd %mydate%
-
itsho about 9 years@AdrianoRepetti: this command creates two subfolders on my machine -
03032015
andTue
-
Adriano Repetti about 9 years@itsho
date
output is locale specific so you may need to adjust to your locale. If, for example, your output is03/03/2015 Tue...
then you'll create more than one folder (one for each "word" indate
output string because final command will be something likemkdir 03032015 Tue
). To fix this you can useeol=
option (to set space as end of line, more robust one) or trimming environment variable usingmkdir %date:~0,8%
. You have other options too (character replacing, for example) but if you need a portable solution you should go with PowerShell...Hope this helps! -
itsho about 9 years@AdrianoRepetti that was truly enlightening. thank you!
-
jeb almost 8 yearsHere are a lot of nearly identical answers, some from 2013. I can't see where your answer adds any improvements. Btw your format even isn't good enough for a simple sort.
-
jelde015 almost 8 yearsThere is improvement in that no one else had it in this format mm-dd-yyyy and when I was reading this page to find the solution for myself it was not obvious how to do so. That is why I decided to share my solution to anyone who wanted this date format styling so they too would not have to go through the trouble changing around these other solutions some of which don't even work. So thank you Jeb for your input but I have to disagree.
-
Delmonte over 7 yearsCorrect command year-month-day:
mkdir %date:~-4,4%"-"%date:~-7,2%"-"%date:~-10,2%
-
PeterCo over 6 yearsFor format yyyy.mm.dd you would use
mkdir %date:~-4,4%"."%date:~-7,2%"."%date:~0,2%
(tested on german Win10) -
user7294900 over 6 yearsWhy sqlplus ? it's irrelevant, also G:, please explain your solution
-
rodolfoprado over 4 yearsi changed a little bit, changing the substrings
-
jeb over 4 yearsWhy? Not for me (and 90% of the world)
-
rodolfoprado over 4 yearsmaybe my regional settings, works better this way; i dont get those "Fri " initial caracteres
-
jeb about 4 years
-
compski about 4 yearswhat happens if I want the date to be 'ddmmyy' instead?