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

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.

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to batch-file

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH CanĀ“t run .bat file under windows 10 Execute a batch file on a remote PC using a batch file on local PC Windows batch - concatenate multiple text files into one How do I create a shortcut via command-line in Windows? Getting Error:JRE_HOME variable is not defined correctly when trying to run startup.bat of Apache-Tomcat Curl not recognized as an internal or external command, operable program or batch file Best way to script remote SSH commands in Batch (Windows)