Note that there are some explanatory texts on larger screens.

plurals
  1. POcan't get variable value in the if statement in windows batch file
    text
    copied!<p>I am trying to write a simple batch file which will control access to the computer (using shutdown) so for every 30 minutes when the computer is on, there has to be at least 30 minutes when the computer is off. The file will be put in auto start. The algorithm looks as follows:</p> <pre><code>Get from the file time when the computer was last closed (LASTCLOSETIME) calculate BREAKUNTIL as LASTCLOSETIME + 30 minutes if CURRENTTIME &lt; BREAKUNTIL { then shutdown now } else { shutdown in 30 minutes save to the file CURRENTTIME + 30 minutes } </code></pre> <p>I did not handle what happens in the first run if the file does not exist, but it is not a problem for now - a file exists and contains valid data.</p> <p>The code I have:</p> <pre><code>::echo off FOR /F "DELIMS=" %%G IN ('TIME /T') DO SET CURRENTTIME=%%G echo %CURRENTTIME% set /A CURRENTTIME = %CURRENTTIME:~0,2%*60 + %CURRENTTIME:~3,2% echo %CURRENTTIME% set /P LASTCLOSETIME=&lt;lastclose.txt set /A BREAKUNTIL = %LASTCLOSETIME% + 30 set /A BREAKUNTIL = %BREAKUNTIL% %% 1440 echo CURRENTTIME: %CURRENTTIME% echo LASTCLOSETIME: %LASTCLOSETIME% echo BREAKUNTIL: %BREAKUNTIL% if "%CURRENTTIME%" LSS "%BREAKUNTIL%" ( shutdown -s -t 30 ) else ( shutdown -s -t 1800 set CLOSETIME = %CURRENTTIME% echo CLOSETIME: %CLOSETIME% set /A CLOSETIME = %CLOSETIME% + 30 set /A CLOSETIME = %CLOSETIME% %% 1440 echo %CLOSETIME% echo %CLOSETIME% &gt; lastclose.txt ) PAUSE </code></pre> <p>The problem is where I set <code>CLOSETIME</code>. It successfully gets the value of <code>CURRENTTIME</code> and I can see e.g. <code>"set CLOSETIME = 886"</code> in the console but the next line does not print this time at all, as if a variable was not set. Does it have something to do with the if statement? I have tried using labels and goto statements but the result was the same.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload