windows - 03/08/2015 was unexpected at this time batch script error -
first, should mention context code, designed check whether automatic maintenance task on computer (or windows refers it, "regular maintenance") had been started yet on same day (if so, last run date , time match date code run on , time 12:00:00pm) , if had finished (if so, task's status "ready" rather "running".
the code store value of each of 3 checks (0 success, 1 failure) in variable called %maintenance completion%, use later decide whether continue next section of code, or whether should loop until conditions true.
for /f "tokens=4" %%a in ('schtasks /query /v /fo list /tn "microsoft\windows\taskscheduler\regular maintenance" ^| findstr /c:"last run time:"') set last_maintenance_date=%%a /f "tokens=5" %%a in ('schtasks /query /v /fo list /tn "microsoft\windows\taskscheduler\regular maintenance" ^| findstr /c:"last run time:"') set last_maintenance_time=%%a /f "tokens=2" %%a in ('schtasks /query /v /fo list /tn "microsoft\windows\taskscheduler\regular maintenance" ^| findstr /c:"status:"') set maintenance_status=%%a /f "tokens=1" %%a in ('date /t') set current_date=%%a if /i %maintenance_date% equ %current_date% (echo date_success & set maintenance_completion=0) else (echo date_fail & set maintenance_completion=1) & set maintenance_completion if /i %maintenance_time% equ 12:00:00 (echo time_success & set maintenance_completion=%maintenance_completion%0) else (echo time_fail & set maintenance_completion=%maintenance_completion%1) & set maintenance_completion if /i %maintenance_status% equ ready (echo status_success & set maintenance_completion=%maintenance_completion%0) else (echo status_fail & set maintenance_completion=%maintenance_completion%1) & set maintenance_completion if %maintenance_completion% equ 000 (echo success) else (echo %maintenance_completion%)
when run code in elevated command prompt window (with %%a being adjusted %a), code runs fine , produces results expect:
c:\windows\system32>for /f "tokens=4" %a in ('schtasks /query /v /fo list /tn "m icrosoft\windows\taskscheduler\regular maintenance" ^| findstr /c:"last run time :"') set last_maintenance_date=%a c:\windows\system32>set last_maintenance_date=01/08/2015 c:\windows\system32>for /f "tokens=5" %a in ('schtasks /query /v /fo list /tn "m icrosoft\windows\taskscheduler\regular maintenance" ^| findstr /c:"last run time :"') set last_maintenance_time=%a c:\windows\system32>set last_maintenance_time=21:16:52 c:\windows\system32>for /f "tokens=2" %a in ('schtasks /query /v /fo list /tn "m icrosoft\windows\taskscheduler\regular maintenance" ^| findstr /c:"status:"') set maintenance_status=%a c:\windows\system32>set maintenance_status=ready c:\windows\system32>for /f "tokens=1" %a in ('date /t') set current_date=%a c:\windows\system32>set current_date=03/08/2015 c:\windows\system32>if /i %maintenance_date% equ %current_date% (echo date_succe ss & set maintenance_completion=0) else (echo date_fail & set maintenance_comple tion=1) date_fail c:\windows\system32>set maintenance_completion maintenance_completion=1 c:\windows\system32>if /i %maintenance_time% equ 12:00:00 (echo time_success & s et maintenance_completion=%maintenance_completion%0) else (echo time_fail & set maintenance_completion=%maintenance_completion%1) time_fail c:\windows\system32>set maintenance_completion maintenance_completion=11 c:\windows\system32>if /i %maintenance_status% equ ready (echo status_success & set maintenance_completion=%maintenance_completion%0) else (echo status_fail & s et maintenance_completion=%maintenance_completion%1) status_success c:\windows\system32>set maintenance_completion maintenance_completion=110 c:\windows\system32>if %maintenance_completion% equ 000 (echo success) else (ech o %maintenance_completion%) 110
however, when run batch script, error:
c:\windows\system32>for /f "tokens=4" %a in ('schtasks /query /v /fo list /tn "m icrosoft\windows\taskscheduler\regular maintenance" | findstr /c:"last run time: "') set last_maintenance_date=%a c:\windows\system32>set last_maintenance_date=01/08/2015 c:\windows\system32>for /f "tokens=5" %a in ('schtasks /query /v /fo list /tn "m icrosoft\windows\taskscheduler\regular maintenance" | findstr /c:"last run time: "') set last_maintenance_time=%a c:\windows\system32>set last_maintenance_time=21:16:52 c:\windows\system32>for /f "tokens=2" %a in ('schtasks /query /v /fo list /tn "m icrosoft\windows\taskscheduler\regular maintenance" | findstr /c:"status:"') set maintenance_status=%a c:\windows\system32>set maintenance_status=ready c:\windows\system32>for /f "tokens=1" %a in ('date /t') set current_date=%a c:\windows\system32>set current_date=03/08/2015 **03/08/2015 unexpected @ time.** c:\windows\system32>**if /i equ 03/08/2015** (echo date_success & set maintenance_c ompletion=0) else (echo date_fail & set maintenance_completion=1) & set maintena nce_completion
in addition, if replace first variable on line fails (%maintenance_date%) equivalent string literal (01/08/2015), line runs fine, , next line fails due same problem first variable on line (%maintenance_time%).
this leads me believe reason, runnning code batch file causes error thrown due having variables first item in if statement, i'm unsure problem or how can fix it?
just figured out wrong! syntax error on part, wrote if /i %maintenance_date%
, if /i %maintenance_time%
rather %last_maintenance_date%
, %last_maintenance_time%
!
once again, suggestion though aschipfl.
Comments
Post a Comment