-1

I have a batch file that's being run with Task Scheduler. This snippet is failing:

Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%
set t=%TIME:~0,2%

mkdir L:\Weekly\%yyyy%%mm%%dd%%t%\Edge

When I initiate the task, this directory is created:

L:\Weekly\2016063013\Edge

However when the task is run via the scheduled trigger, this directory is created:

L:\Weekly\20160702

I'm having a hard time debugging this error. I need the hour to differentiate between backups that were manually triggered after an event vs. the normal nightly/weekly automated routines.

EricP
  • 502
  • 2
  • 14
  • you mean `L:\Weekly\20160702\Edge` right? When did the task run ? July the 2nd? – Jean-François Fabre Jul 21 '16 at 20:11
  • 2
    If triggered before 10:00 a.m. then there could be a leading space in %time%. Enclose path in mkdir in a pair of double quotes and/or add set "t=%t: =0%" – JosefZ Jul 21 '16 at 20:13
  • Using `date` and `time` is also locale dependent. For a locale independent solution use `wmic` instead. See my answer [Print datetime in Windows cmd](http://superuser.com/a/1045459) – DavidPostill Jul 21 '16 at 21:40
  • @JosefZ you must be right and post an answer. It creates (or tries to create) 2 directories, which also explains the missing `Edge` stuff. Damn spaces. – Jean-François Fabre Jul 21 '16 at 21:45
  • I'm trying @JosefZ 's solution tonight. – EricP Jul 21 '16 at 22:33
  • @Jean-François Fabre , that is literally what it created. – EricP Jul 21 '16 at 22:33
  • @JosefZ that was it. It worked perfectly for the first time since I added the time to the folder name. Post it as the answer? – EricP Jul 22 '16 at 13:51

1 Answers1

1

@JosefZ solved this for me in a comment to my question.

The script failed to run before noon because when I pulled the hour with %TIME:~0,2%, that value was padded with a space, i.e. " 2". That is why makedir did not include the "\Edge" subdirectory.

Here is the corrected snippet:

Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%
Set t=%TIME:~0,2%

::replace space with a zero
Set t=%t: =0%

mkdir L:\Weekly\%yyyy%%mm%%dd%%t%\Edge
EricP
  • 502
  • 2
  • 14