无法在Windows批处理文件的if语句中获取变量值 [英] can't get variable value in the if statement in windows batch file

查看:244
本文介绍了无法在Windows批处理文件的if语句中获取变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的批处理文件,该文件将控制对计算机的访问(使用关机),因此在计算机打开时每30分钟必须在计算机关闭时至少有30分钟.该文件将被放入自动启动.该算法如下所示:

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:

Get from the file time when the computer was last closed (LASTCLOSETIME)
calculate BREAKUNTIL as LASTCLOSETIME + 30 minutes
if CURRENTTIME < BREAKUNTIL {
    then shutdown now
} else {
    shutdown in 30 minutes
    save to the file CURRENTTIME + 30 minutes
}

如果文件不存在,我不会处理第一次运行时发生的情况,但是现在这不是问题-文件存在并且包含有效数据.

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.

我拥有的代码:

::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=<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% > lastclose.txt
)

PAUSE

问题是我设置了CLOSETIME的位置.它成功获取CURRENTTIME的值,我可以看到例如在控制台中"set CLOSETIME = 886",但下一次根本不打印下一行,就像未设置变量一样.它与if语句有关吗?我尝试使用标签和goto语句,但是结果是相同的.

The problem is where I set CLOSETIME. It successfully gets the value of CURRENTTIME and I can see e.g. "set CLOSETIME = 886" 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.

推荐答案

标准的DELAYEDEXPANSION问题.

Standard DELAYEDEXPANSION problem.

这是一个复合语句:

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% > lastclose.txt
)

当语句为PARSED时,将替换任何%var%的CURRENT值,并执行 THEN 语句.直到该语句被执行,该值才分配给closetime,因此该语句被解释为

When the statement is PARSED, the CURRENT values of any %var% are substituted and THEN the statement is executed. The value isn't ASSIGNED to closetime until the statement is EXECUTED, hence the statement is interpreted as

if "%CURRENTTIME%" LSS "%BREAKUNTIL%" (
    shutdown -s -t 30
) else (
    shutdown -s -t 1800

    set CLOSETIME = %CURRENTTIME%
    echo CLOSETIME: 
    set /A CLOSETIME = + 30
    set /A CLOSETIME =  %% 1440

    echo 
    echo  > lastclose.txt
)

最简单的治疗方法是将此顺序更改为

Easiest cure is to change this sequence to

if "%CURRENTTIME%" LSS "%BREAKUNTIL%" (
    shutdown -s -t 30
    GOTO :EOF
)
shutdown -s -t 1800

set CLOSETIME = %CURRENTTIME%
echo CLOSETIME: %CLOSETIME%
set /A CLOSETIME = %CLOSETIME% + 30
set /A CLOSETIME = %CLOSETIME% %% 1440

echo %CLOSETIME%
echo %CLOSETIME% > lastclose.txt

请注意,冒号为requiredGOTO :EOF表示'转到文件的物理结尾.

Note that GOTO :EOF where the colon is required means 'go to the physical end-of-file.

第二个最简单的方法是执行

Second easiest is to execute

SETLOCAL ENABLEDELAYEDEXPANSION

在此IF语句之前的某个位置(通常直接在@echo off之后),然后将IF更改为

somewhere before this IF statement (commonly directly after the @echo off) and change the IF to

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! > lastclose.txt
)

注意,当DELAYEDEXPANSION有效时,!var!返回var的RUNTIME值(即更改),而%var%返回var的PARSE-TIME值(即语句在执行之前就已经解析了.)

Noting that when DELAYEDEXPANSION is in effect, !var! returns the RUNTIME value of var (ie as it changes) whereas %var% returns the PARSE-TIME value of var (ie as it stood when the statement was parsed, before execution.)

这篇关于无法在Windows批处理文件的if语句中获取变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆