当前位置: 动力学知识库 > 问答 > 编程问答 >

wait until command is finished batch

问题描述:

In my batch script I have the following:

if somecondition (

exit

) 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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: