5

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

DextrousDave
  • 6,603
  • 35
  • 91
  • 134

4 Answers4

23

%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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
7
for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"
Endoro
  • 37,015
  • 8
  • 50
  • 63
2

Do this:

for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd1="%%k"_"%%i"_"%%j"
mkdir %yyyymmdd1%
ilansch
  • 4,784
  • 7
  • 47
  • 96
  • 1
    taken from a previous answer that might help you, http://stackoverflow.com/questions/11280077/batch-script-to-make-backup-folder-only-new-and-modified-files/ - just see how i manipulate the "%%i" or %%k or %%j and the _ between them – ilansch Jun 19 '13 at 06:35
  • thanks, but your answer only shows the month and day in the folder name, not the year...test it – DextrousDave Jun 19 '13 at 07:03
  • hi, it shows it all maybe you didnt copy paste the whole line :) my answer is as the one you accepted. it is identical and original – ilansch Jun 19 '13 at 07:32
2

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

Community
  • 1
  • 1
Dejan Dakić
  • 2,418
  • 2
  • 25
  • 39