DextrousDave
DextrousDave

Reputation: 6793

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

Upvotes: 5

Views: 53899

Answers (4)

Dejan Dakić
Dejan Dakić

Reputation: 2448

or simply

SET Today=%Date:~10,4%_%Date:~7,2%_%Date:~4,2%

echo %today%

outputs

2013_06_19
Press any key to continue . . .

Then you could easily use the variable today for directory creation e.g.:

mkdir %today%

EDIT: YYYY_MM_DD format

Upvotes: 2

foxidrive
foxidrive

Reputation: 41234

%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

Upvotes: 23

Endoro
Endoro

Reputation: 37569

for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"

Upvotes: 7

ilansch
ilansch

Reputation: 4878

Do this:

for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd1="%%k"_"%%i"_"%%j"
mkdir %yyyymmdd1%

Upvotes: 2

Related Questions