46

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
  • 15,343
  • 26
  • 90
  • 111
  • Does 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?](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) – feeela Mar 23 '20 at 11:31

25 Answers25

42
mkdir %date:~-4,4%%date:~-10,2%%date:~7,2%
Stefano Travelli
  • 1,821
  • 1
  • 15
  • 18
22

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.

Joey
  • 316,376
  • 76
  • 642
  • 652
  • 1
    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%` – mikequentel Aug 21 '14 at 18:19
21

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

Franck Freiburger
  • 21,662
  • 20
  • 60
  • 90
  • 25
    To make it more easy: `mkdir %date:/=%` – Adriano Repetti Apr 09 '13 at 12:48
  • 3
    @Adriano: impressive. Could you elaborate a bit on the `:/=` thing? Opened a question for this: http://stackoverflow.com/q/21032288/520162 – eckes Jan 09 '14 at 22:10
  • 2
    @AdrianoRepetti: this command creates **two** subfolders on my machine - `03032015` and `Tue` – itsho Mar 03 '15 at 06:54
  • 2
    @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! – Adriano Repetti Mar 03 '15 at 08:16
  • @AdrianoRepetti that was truly enlightening. thank you! – itsho Mar 03 '15 at 08:26
  • 8
    Correct command year-month-day: `mkdir %date:~-4,4%"-"%date:~-7,2%"-"%date:~-10,2%` – Delmonte Sep 21 '16 at 14:37
16

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%
Simon G.
  • 6,269
  • 22
  • 29
  • surprisingly this is creating folder with name `T2011`. There is not month and day in it. – Ramesh Soni Mar 30 '11 at 11:57
  • 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. Mar 30 '11 at 12:09
  • 1
    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. Mar 30 '11 at 12:13
  • well I can't get that to format properly - I will add a new answer – Simon G. Mar 30 '11 at 12:16
13

If you want mm-dd-yyyy format you can use:

mkdir %date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%
jeb
  • 70,992
  • 15
  • 159
  • 202
jelde015
  • 467
  • 6
  • 6
  • 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. – jeb Jun 14 '16 at 14:48
  • 1
    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. – jelde015 Jun 20 '16 at 17:02
  • 1
    For format yyyy.mm.dd you would use `mkdir %date:~-4,4%"."%date:~-7,2%"."%date:~0,2%` (tested on german Win10) – PeterCo Aug 14 '17 at 09:34
9

This depends on the regional settings of the computer, so first check the output of the date using the command prompt or by doing an echo of date.

To do so, create a batch file and add the below content

echo %date%    
pause

It produces an output, in my case it shows Fri 05/06/2015.

Now we need to get rid of the slash (/)

For that include the below code in the batch file.

set temp=%DATE:/=%

if you echo the "temp", you can see the date without the slash in it.


Now all you need to do is formatting the date in the way you want.

For example I need the date in the format of YYYYMMDD, then I need to set the dirname as below

To explain how this works, we need to compare the value of temp

Fri 05062015.

now position each characters with numbers starting with 0.

Fri 0506201 5

01234567891011

So for the date format which I need is 20150605,

The Year 2015, in which 2 is in the 8th position, so from 8th position till 4 places, it will make 2015.

The month 06, in which 0 is in the 6th position, so from 6th position till 2 places, it will make 06.

The day 05, in which 0 is in the 4th position, so from 4th position till 2 places, it will make 05.

So finally to set up the final format, we have the below.

SET dirname="%temp:~8,4%%temp:~6,2%%temp:~4,2%"

To enhance this date format with "-" or "_" in between the date, month and year , you can modify with below

SET dirname="%temp:~8,4%-%temp:~6,2%-%temp:~4,2%"

or

SET dirname="%temp:~8,4%_%temp:~6,2%_%temp:~4,2%"

So the final batch code will be

======================================================

@echo off    
set temp=%DATE:/=%
set dirname="%temp:~8,4%%temp:~6,2%%temp:~4,2%"
mkdir %dirname%

======================================================

The directory will be created at the place where this batch executes.

Gerhard
  • 18,114
  • 5
  • 20
  • 38
nirmalraj17
  • 474
  • 7
  • 18
8
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.
  • 6,269
  • 22
  • 29
5
for /F “tokens=1-4 delims=/ ” %%A in (‘date /t’) do (
    set DateDay=%%A
    set DateMonth=%%B
    set DateYear=%%C
)
set CurrentDate=%DateDay%-%DateMonth%-%DateYear%
md %CurrentDate%

This will give you a newly created folder with today’s date, in the format of DD-MM-YY

Sourced from: Ali's Knowledge Base

Ren
  • 1,103
  • 5
  • 15
  • 24
Mark snow
  • 51
  • 1
  • 1
4

I had a problem with this because my server ABSOLUTELY had to have its date in MM/dd/yyyy format, while I wanted the directory to be in YYYY-MM-DD format for neatness sake. Here's how to get it in YYYY-MM-DD format, no matter what your regional settings are set as.

Find out what gets displayed when you use %DATE%:

From a command prompt type:

ECHO %DATE%

Mine came out 03/06/2013 (as in 6th March 2013)

Therefore, to get a directory name as 2013-03-06, code this into your batch file:

SET dirname="%date:~6,4%-%date:~0,2%-%date:~3,2%"
mkdir %dirname%
3

This should work:

mkdir %date%

If it doesn't, try this:

setlocal enableextensions
mkdir %date%
Blorgbeard
  • 93,378
  • 43
  • 217
  • 263
  • @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 Mar 30 '11 at 11:55
  • @Ramesh, what does `echo %date%` output? It will depend on your regional settings - mine says "2011-03-30" – Blorgbeard Mar 30 '11 at 11:58
  • @Blorgbeard even the folder name is created as T2011. I am using standard US English culture. Is there anything I am missing? – Ramesh Soni Mar 30 '11 at 12:02
  • @Ramesh, does `echo %date%` output `T2011` and nothing else? – Blorgbeard Mar 30 '11 at 12:05
  • @Blorgbeard yes just T2011 and nothing else. – Ramesh Soni Mar 30 '11 at 16:04
  • @Ramesh OK, then I am stumped, sorry :) [Simon's answer](http://stackoverflow.com/questions/5485853/how-to-create-a-folder-with-name-as-current-date-in-batch-bat-files/5486137#5486137) looks promising.. – Blorgbeard Mar 30 '11 at 20:32
2

I am sitting in exactly the same boat as you as soon as i am AM before 10 i cannot use the below, i have set my time from 12hr to 24 hr, changed hh/mm to HH/mm I have tried most of the codes i could find. below will help at least a little. tweak and fix :)

Below may help also

set DD=%DATE:~0,2%

set MM=%DATE:~3,2%

set YY=%DATE:~8,2%

set YYYY=%DATE:~6,4%

set hh=%hh: =0%

set mm=%TIME:~3,2%

if "%time:~0,1%" == " " (set folderdate=0%time:~1,1%) ELSE set folderdate=%time:~0,2%

mkdir folderdate=%date:~6%%date:~3,2%%date:~0,2%_%folderdate%%time:~3,2%

copy \Makereport*.CSV \Makereport\%folderdate%\

cd %folderdate% REM -( 7zip in c:\batch) Path = c:\batch

7z a Retail.zip *.CSV -pRetailPassword

cd..

del *.csv

Bize32
  • 53
  • 6
2

the expression %date:~p,n% returns n number of characters from position p in the date string.

if my system date string is Mon23/11/2015

the command %date:~1,3% returns the value Mon

the command %date:~10,4% returns the value 2015

and in conjunction with the md (or mkdir) command

the command md %date:~10,4%%date:~7,2%%date:~4,2% makes a directory named 20151123

likewise if your date string in in the format Monday, 23/Nov/2015

the command md %date:~16,4%%date:~12,3%%date:~9,2% makes a directory named 2015Nov23

If you accidentally return characters from the date string that are not allowed in folder names or use invalid values for p and n you will get an error. Additionally if you return values that include \ this may create a folder within a folder.

Leigh
  • 21
  • 2
2

If your locale has date format "DDMMYYYY" you'll have to set it this way:

set datestr=%date:~-4,4%%date:~3,2%%date:~-10,2%
mkdir %datestr%
Marco
  • 393
  • 6
  • 13
1

this is a more simpler solution.

@ECHO OFF
set name=%date%
echo %name%
mkdir %name% 
1

This works for me, try:

ECHO %DATE:~7,2%_%DATE:~4,2%_%DATE:~12,2%
Java Devil
  • 9,835
  • 7
  • 30
  • 44
Natan Braslavski
  • 638
  • 5
  • 18
1

You'll like this, change it so that it can suit your requirements.

mkdir today
Copy Desktop\test1\*.* today
setlocal enableextensions
set name=%DATE:/=_%
Rename "today" _OlddatabaseBackup_"%name%"
Ajay Kulkarni
  • 2,546
  • 8
  • 39
  • 80
1

Thanks for the info all, very helpful. I needed something that could create "backup" folder as often as every minute in the same directory, as well as call on it later in the script. Here's what I came up with:

@ echo off

CD %userprofile%\desktop

SET Datefolder="%DATE:~4,2%-%DATE:~7,2%-%DATE:~12,2%_%time:~1,1%%time:~3,2%"

MD "%Datefolder%"

This gives me a folder on the currently logged on user's desktop named: mm-dd-yy_hmm (hour minute minute) ie: 07-28-15_719

lowtechsun
  • 1,644
  • 3
  • 22
  • 49
pshopgeek
  • 11
  • 1
1

I use the next code to make a file copy (e.g. test.txt) before replacing:

cd /d %~dp0
set backupDir=%date:~7,2%-%date:~-10,2%-%date:~-2,2%_%time:~0,2%.%time:~3,2%.%time:~6,2%
echo make dir %backupDir% ...
md "%backupDir%"
copy test.txt %backupDir%

It creates directory in format DD-MM-YY_HH.MM.SS and places text.txt there. Time with seconds in the name is necessary to create directory without additional verification.

Proxyma
  • 13
  • 5
0

https://stackoverflow.com/a/31789045/1010918 foxidrive's answer helped me get the folder with the date and time I wanted. I would like to share this method here since it worked great for me and I think it could help other people too, regardless of their locale.

rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"

 set "dirname=%YYYY%-%MM%-%DD% %HH%-%Min%-%Sec%"

 :: remove echo here if you like
 echo "dirName"="%dirName%"
Community
  • 1
  • 1
lowtechsun
  • 1,644
  • 3
  • 22
  • 49
0

Use this batch script made by me:

@echo off
title Folder Creator
color b
setlocal enabledelayedexpansion
echo Enter the folder name, you can use these codes:
echo /t - Time (eg. 16:29)
echo /d - Date (eg. 17-02-19)
echo /a - Day (eg. 17)
echo /m - Month (eg. 02)
echo /y - Year (eg. 19)
echo /f - Full Year (eg. 2019)
echo.
set /p foldername=Folder Name:
set foldername=%foldername:/t=!time:~0,5!%
set foldername=%foldername:/d=!date:~0,2!-!date:~3,2!-!date:~8,2!%
set foldername=%foldername:/a=!date:~0,2!%
set foldername=%foldername:/m=!date:~3,2!%
set foldername=%foldername:/y=!date:~8,2!%
set foldername=%foldername:/f=!date:~6,4!%
md %foldername%

For example if you wanted to make a folder named the date in the DD-MM-YY format you would type "/d" but if you wanted to do that in the DD-MM-YYYY format you would type "/a-/m-/f".

Hayz
  • 1
0

this worked better for me,

@echo off    
set temp=%DATE:/=%
set dirname="%temp:~4,4%%temp:~2,2%%temp:~0,2%"
mkdir %dirname%
rodolfoprado
  • 116
  • 7
0

For YYYY.MM.DD format with HUN settings use following. It converts "2021. 02. 23." to "2021.02.23":

SET dirname="%date:~0,5%%date:~6,3%%date:~10,2%"
md %dirname%
Bence Gacsályi
  • 306
  • 2
  • 4
-1
G:

cd G:/app/

mkdir %date:~7,2%%date:~-10,2%%date:~-4,4% 

cd %date:~7,2%%date:~-10,2%%date:~-4,4% 

sqlplus sys/sys as sysdba @c:/new
Kayvan Mazaheri
  • 1,900
  • 18
  • 33
-1

I needed both the date and time and used:

mkdir %date%-%time:~0,2%.%time:~3,2%.%time:~6,2%

Which created a folder that looked like: 2018-10-23-17.18.34

The time had to be concatenated because it contained : which is not allowed on Windows.

andre
  • 614
  • 6
  • 12
-1
setlocal enableextensions 
set name="%DATE:/=_%"
mkdir %name%

to create only one folder like "Tue 01_28_2020"

  • This answer seems to be a duplicate, compare [comment](https://stackoverflow.com/questions/5485853/how-to-create-a-folder-with-name-as-current-date-in-batch-bat-files/59956772#comment22645893_5485929) or [Simon G.](https://stackoverflow.com/a/5485931/463115) – jeb Jan 29 '20 at 16:11