0

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.

Arnold
  • 185
  • 1
  • 2
  • 8

1 Answers1

3

"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)

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks. It worked. But instead of the folder name ->20160810-945 ,can I get like -> 20160810-0945 – Arnold Aug 10 '16 at 17:00
  • this should do. If not, please let me know your output of `echo -%time%-` (before 10:00 am please) – Stephan Aug 10 '16 at 17:02
  • OK I will test it before 10.00 am. by the way when I tried mkdir "%mydate%-%mytime: =0%" it did not create a folder, it create a file with name yyyymmdd- – Arnold Aug 10 '16 at 17:10
  • wait - `mkdir` is creating a *file*?? There is something fundametaly wrong. Do you happen to have a `mkdir.bat`? – Stephan Aug 10 '16 at 18:35