How to create a folder with name as current date in batch (.bat) files

268,647

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%
Share:
268,647

Related videos on Youtube

Ramesh Soni
Author by

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, 2021

Comments

  • Ramesh Soni
    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?

  • Ramesh Soni
    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
    Ramesh Soni about 13 years
    surprisingly this is creating folder with name T2011. There is not month and day in it.
  • Blorgbeard
    Blorgbeard about 13 years
    @Ramesh, what does echo %date% output? It will depend on your regional settings - mine says "2011-03-30"
  • Blorgbeard
    Blorgbeard about 13 years
    Heavily dependent on regional settings
  • Ramesh Soni
    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
    Blorgbeard about 13 years
    @Ramesh, does echo %date% output T2011 and nothing else?
  • Simon G.
    Simon G. about 13 years
    If 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.
    Simon G. about 13 years
    Having 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.
    Simon G. about 13 years
    well I can't get that to format properly - I will add a new answer
  • Ramesh Soni
    Ramesh Soni about 13 years
    @Blorgbeard yes just T2011 and nothing else.
  • Blorgbeard
    Blorgbeard about 13 years
    @Ramesh OK, then I am stumped, sorry :) Simon's answer looks promising..
  • Adriano Repetti
    Adriano Repetti about 11 years
    To make it more easy: mkdir %date:/=%
  • eckes
    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
    Bernhard Hofmann over 10 years
    I'd +1 that if you used %TEMP% or %tmp% as the path and if you deleted the temporary files. :)
  • mikequentel
    mikequentel over 9 years
    Excellent 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 by md %mydate%
  • itsho
    itsho about 9 years
    @AdrianoRepetti: this command creates two subfolders on my machine - 03032015 and Tue
  • Adriano Repetti
    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 is 03/03/2015 Tue... then you'll create more than one folder (one for each "word" in date output string because final command will be something like mkdir 03032015 Tue). To fix this you can use eol= option (to set space as end of line, more robust one) or trimming environment variable using mkdir %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
    itsho about 9 years
    @AdrianoRepetti that was truly enlightening. thank you!
  • jeb
    jeb almost 8 years
    Here 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
    jelde015 almost 8 years
    There 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
    Delmonte over 7 years
    Correct command year-month-day: mkdir %date:~-4,4%"-"%date:~-7,2%"-"%date:~-10,2%
  • PeterCo
    PeterCo over 6 years
    For format yyyy.mm.dd you would use mkdir %date:~-4,4%"."%date:~-7,2%"."%date:~0,2% (tested on german Win10)
  • user7294900
    user7294900 over 6 years
    Why sqlplus ? it's irrelevant, also G:, please explain your solution
  • rodolfoprado
    rodolfoprado over 4 years
    i changed a little bit, changing the substrings
  • jeb
    jeb over 4 years
    Why? Not for me (and 90% of the world)
  • rodolfoprado
    rodolfoprado over 4 years
    maybe my regional settings, works better this way; i dont get those "Fri " initial caracteres
  • jeb
    jeb about 4 years
    This answer seems to be a duplicate, compare comment or Simon G.
  • compski
    compski about 4 years
    what happens if I want the date to be 'ddmmyy' instead?