0

I'm trying to have a batch file to perform different task based on hours (24h clock). It works, but not on every hour which is strange and I can't figure it out. What am I doing wrong here, could anyone help out? So if the clock is 2:33 it goes to :hour2 but when it's 4:33 it gives an error "(goto was unexpected at this time."

For /f %%# In ('WMIC Path Win32_LocalTime Get Hour^|Findstr [0-23]') Do (Set DOW=%%#)
echo %DOW% >NUL
IF %DOW%==0 (goto hour0)
IF %DOW%==1 (goto hour1)
IF %DOW%==2 (goto hour2)
IF %DOW%==3 (goto hour3)
IF %DOW%==4 (goto hour4)
IF %DOW%==5 (goto hour3)
IF %DOW%==6 (goto hour3)
IF %DOW%==7 (goto hour3)
IF %DOW%==8 (goto hour3)
IF %DOW%==9 (goto hour9)
IF %DOW%==10 (goto hour10)
IF %DOW%==11 (goto hour11)
IF %DOW%==12 (goto hour12)
IF %DOW%==13 (goto hour13)
IF %DOW%==14 (goto hour14)
IF %DOW%==15 (goto hour15)
IF %DOW%==16 (goto hour16)
IF %DOW%==17 (goto hour17)
IF %DOW%==18 (goto hour18)
IF %DOW%==19 (goto hour19)
IF %DOW%==20 (goto hour20)
IF %DOW%==21 (goto hour21)
IF %DOW%==22 (goto hour22)
IF %DOW%==23 (goto hour23)

:hour0
echo it's now hour 0
goto finish

...[All hours repeated exactly the same]...

:finish

I expected WMIC Path Win32_LocalTime Get Hour to work with this one. As standalone it results string "Hour 4", but the batch file doesn't somehow recognize all the hours.

Handyy
  • 13
  • 2
  • 1
    [This may provide a clue](https://stackoverflow.com/a/76244289/2128947) - and why not just `goto hour%dow%` ? – Magoo May 16 '23 at 02:16
  • 1
    Standalone wmic (no findstr) should be `Hour` _linebreak_ `4` (during that hour period) -- is that what you meant? Standalone wmic _with_ your findstr won't show the 4. Try `for /L %n in (0,1,23) do @echo %n | findstr [0-23]` (double % if in a batch file) and you'll see that values 4-9 are unselected by that command. `[...]` in a regexp matches each character individually not as a numeric string. PS: CMD also has `%time:~0,2%` but I'm not sure if it depends on your 'region' setting (`%date%` does) – dave_thompson_085 May 16 '23 at 03:45
  • Do not use anymore `%SystemRoot%\System32\wbem\wmic.exe` in newly written batch scripts because of Microsoft declared this executable already as deprecated and it is not installed anymore on currently latest Windows 11 on being fresh installed and not upgraded from a former Windows version. I recommend to use `for /F "tokens=2 delims=: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do goto hour%%I` and define the labels with `hour00`, `hour01` ... `hour23`. The leading `0` for `00` to `09` is important! That command line works from Windows Vista to latest Windows 11. – Mofi May 16 '23 at 06:48
  • For an explanation of the command line read Compo's or my answer on [Time is set incorrectly after midnight](https://stackoverflow.com/questions/60124351/time-is-set-incorrectly-after-midnight/). BTW: `DOW` means day of week (Monday, Tuesday, ...) which is definitely not a good name for an environment variable with current hour as value. PS: The regular expression `[0-23]` is interpreted as search for a character being `0` or `1` or `¹` (FINDSTR special) or `2` or `3`. – Mofi May 16 '23 at 06:49
  • I replaced the batch file code with the robocopy.exe and that works perfectly, thank you! – Handyy May 16 '23 at 09:02

0 Answers0