Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 批处理文件未能在IF子句中设置变量_Variables_Batch File_Set - Fatal编程技术网

Variables 批处理文件未能在IF子句中设置变量

Variables 批处理文件未能在IF子句中设置变量,variables,batch-file,set,Variables,Batch File,Set,即使发生匹配,以下代码也不会将运行更新为等于N。这意味着我不会加入呼叫代码。我是不是遗漏了什么 SET Run=Y REM Check current files date/time information and establish if the file has been present too long in the directory REM Skip first 4 lines as header information not required FOR /f "tokens=1-

即使发生匹配,以下代码也不会将运行更新为等于N。这意味着我不会加入呼叫代码。我是不是遗漏了什么

SET Run=Y

REM Check current files date/time information and establish if the file has been present too long in the directory
REM Skip first 4 lines as header information not required

FOR /f "tokens=1-5 skip=4 delims= " %%G IN ('dir /OD "%SRCH_CRITERIA% "') DO (

    ECHO "Params to processFile:  " %%G %%H %%I ""%%K""
    IF %%K.==.  ( 
        ECHO "K:nothing"
        SET Run=N
        ECHO %Run%
    ) 

    IF %%K==free (
        ECHO "K:FREE"
        SET Run=N
        ECHO %Run%
    ) 

    ECHO %Run% RUN
    IF %Run%=="Y" (
        CALL :processFile "%%G" "%%H" "%%I" "%%K"
    )   
)

您需要使用cmd.exe的延迟扩展选项

在脚本的顶部,放置:

setlocal enableextensions enabledelayedexpansion
然后把:

endlocal
在底部

然后你需要使用
!快跑而不是
%Run%

代码无法工作的原因是在遇到整个FOR语句(包括其中的命令)时会对其进行求值。这就是
%Run%
变量展开的地方

通过使用延迟扩展,在实际需要它们之前(在块中设置它们之后)不会扩展它们

您可以看到此脚本中的差异:

@echo off
setlocal enableextensions enabledelayedexpansion

set xx=0
for %%i in (a b c d) do (
    echo %%i
    set /a "xx = xx + 1"
    if %xx%==3 echo yes for normal
    if !xx!==3 echo yes for delayed
)

endlocal
哪些产出:

a
b
c
yes for delayed
d

您会注意到,使用
%xx%
进行检查不起作用,因为这是在
for
语句启动时(并且
xx
为0)计算的。延迟的扩展
!xx!确实有效,因为每次通过循环都会对其进行评估。

谢谢paxdiablo,我的问题完全解决了