In my batch script I have the following:

if somecondition (


) else (

for /F "USEBACKQ tokens=4,6,8" %%a in (systeminfo ^| qgrep -e "System Up Time:") do set days=%%a&set hours=%%b&set minutes=%%c

set /A timepassed=%days%*24*60+%hours%*60+%minutes%

if "%timepassed%" leq 30 (

echo %timepassed%

) else (

echo %time$



is there any way to make sure that the variable timepassed is set before it is compared to the value 30?

At times, if this portion of code is run, timepassed seems to be evaluated at to be "" instead of what it is supposed to be.

Thanks for the time


Well, you're setting an environment variable in a block and reading it in the same block. This cannot work because cmd expands environment variables when a statement is parsed, not when it's executed (and a block is a statement, so inside the block %timepassed% is already replaced by its value before you even enter the block).

To move variable expansion to runtime of a statement add

setlocal enabledelayedexpansion

at the start of your batch and use !timepassed! instead %timepassed%. Likewise for other variables that are set and used in the block.