Arnold
Arnold

Reputation: 185

creating a folder name with a batch file

I am trying to create a folder using batch file. The folder name should be in a format - yyyymmdd-hhmm .I got started with the below code but I get yyyymmdd- as one folder and hhmm as another folder. But when I tried it after 13.00 hrs I get yyyymmdd-hhmm format. Why is there a different behaviour during 9:45 in the morning. I don't know. Any help appreciated.

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
mkdir %mydate%-%mytime%

I get 1 folder -> 20160810- and another folder -> 945.

Upvotes: 0

Views: 1052

Answers (1)

Stephan
Stephan

Reputation: 56180

"I get 1 folder -> 20160810- and another folder -> 945."

That's because of the space, so mkdir sees two parameters and so creates two folders.

Either put qoutes around the new foldername

mkdir "%mydate%-%mytime%"` 

or (maybe better) replace the space with a zero:

mkdir %mydate%-%mytime: =0%

putting qoutes around anyway doesn't harm:

mkdir "%mydate%-%mytime: =0%"

(btw: there is a way to get a date-time-string independent of local settings)

Upvotes: 3

Related Questions