Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 885
Ramesh Soni Avatar asked Mar 30 '11 11:03

Ramesh Soni


People also ask

How do I create a folder for today's date?

For a Windows batch-file, you'd just use mkdir "%DATE%" or mkdir "%datestamp%" -- whichever variable you want to use.

What is %% in a batch 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.


2 Answers

mkdir %date:~-4,4%%date:~-10,2%%date:~7,2%
like image 163
Stefano Travelli Avatar answered Oct 08 '22 07:10

Stefano Travelli


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.

like image 22
Joey Avatar answered Oct 08 '22 07:10

Joey