1

I have a problem with the code I have written and been trying to run in batch. Here it is:

echo off

SETLOCAL

set a==%time /t%
if (a!=18:00) goto stop
else goto start

:start
net stop "spooler"
timeout 3 > null
echo "it is done"
timeout 5 > null
echo "now we restarting the service"
timeout 3 > null
net start "spooler"

:stop
PAUSE
ENDLOCAL  

What it was supposed to do is to check the current time on machine verify with the one pre-conditioned in the code and if it does not match or does, act accordingly. I don't know why (probably because of me being the novice) it returns the error message which says:

=18:00 was unexpected at this time

Any suggestions?

Thanks in advance,

R. Shahin

mayersdesign
  • 5,062
  • 4
  • 35
  • 47

1 Answers1

0

To assign result of command to variable you need to use for /f If command does not support c-like syntax.Besides you have built-in %time% variable. you need to use something like if %my_time% neq 18:00 . Mind that %time% (and time command) are dependent on the time settings. Try something like this:

@echo off
pushd "%temp%"
makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do (
   set "current-time=%%d"

)
del ~.*
popd

set hours=%current-time:~0,2%
set minutes=%current-time:~3,2%

echo %hours%%minutes%

if "%hours%%minutes%" neq "1800" (
  goto stop
) else (
  goto start
)
....
Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Thank you. I have added your code and run it and it returned with the error message saying: " | was unexpected at this time " – Shahin Rzayev Mar 27 '17 at 07:25
  • @echo off pushd "%temp%" makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nul for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do ( set "current-time=%%d" ) del ~.* popd set hours=%current-time:~0,2% set minutes=%current-time:~3,2% echo %hours%%minutes% if "%hours%%minutes%" neq "1125" ( goto stop ) else ( goto start ) :start net stop "spooler" timeout 3 > null echo "it is done" timeout 5 > null echo "now we restarting the service" timeout 3 > null net start "spooler" :stop PAUSE ENDLOCAL – Shahin Rzayev Mar 27 '17 at 07:26