Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Prompt/Bat file - Create new folder named with today's date

I use the following code to create a new folder that is named with todays date:

for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir c:\%date:/=%

Now the format is as follows:

20130619

How do I change the format to?:

2013_06_19

Thank you

like image 397
DextrousDave Avatar asked Jun 19 '13 06:06

DextrousDave


People also ask

What is %% in a BAT file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do I get the current date and time in CMD?

On a Microsoft Windows system, you can obtain the current date using the date /t command (the /t option prevents the command from prompting for a change to the the date) or by using echo %date% to display the contents of the date environment variable.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.


2 Answers

%date% depends on your computer settings, and locale. Here is a reliable way to get a date and time stamp. Win XP pro and above.

If you need to use your batch file on unknown machines then this is worth using.

:: time and date stamp YYYYMMDD, HHMMSS and YYYY-MM-DD_HH-MM-SS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
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 stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"

pause
like image 100
foxidrive Avatar answered Oct 17 '22 06:10

foxidrive


for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"
like image 28
Endoro Avatar answered Oct 17 '22 05:10

Endoro