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
Batch file 批处理脚本比较问题 如果存在“C:\Windows\System32\updatevmcheck.txt”( set/p Build=C:\Windows\System32\updatevmcheck.txt 出口 ) 否则( echo 1>C:\Windows\System32\updatevmcheck.txt 出口 )_Batch File - Fatal编程技术网

Batch file 批处理脚本比较问题 如果存在“C:\Windows\System32\updatevmcheck.txt”( set/p Build=C:\Windows\System32\updatevmcheck.txt 出口 ) 否则( echo 1>C:\Windows\System32\updatevmcheck.txt 出口 )

Batch file 批处理脚本比较问题 如果存在“C:\Windows\System32\updatevmcheck.txt”( set/p Build=C:\Windows\System32\updatevmcheck.txt 出口 ) 否则( echo 1>C:\Windows\System32\updatevmcheck.txt 出口 ),batch-file,Batch File,以上是我目前的代码。基本上,它会检查文件是否存在,如果存在,它会检查文件中的数字是否小于您指定的数字。如果是这样,它会运行一些代码,然后更新文件中的编号,然后退出。否则,它将创建带有数字的文件,然后退出。我相信我的语法是正确的,我可以运行单独的行,但是当我创建批处理文件时,它似乎甚至没有通过if-exist语句。除了糟糕的格式之外,有人能看出这有什么明显的错误吗:) 您正在块内设置变量,并在同一块中使用其值。这不能用于普通变量扩展(在解析命令(包括完整的块)时发生,而不是在运行时发生)。要解决此

以上是我目前的代码。基本上,它会检查文件是否存在,如果存在,它会检查文件中的数字是否小于您指定的数字。如果是这样,它会运行一些代码,然后更新文件中的编号,然后退出。否则,它将创建带有数字的文件,然后退出。我相信我的语法是正确的,我可以运行单独的行,但是当我创建批处理文件时,它似乎甚至没有通过if-exist语句。除了糟糕的格式之外,有人能看出这有什么明显的错误吗:)

您正在块内设置变量,并在同一块中使用其值。这不能用于普通变量扩展(在解析命令(包括完整的块)时发生,而不是在运行时发生)。要解决此问题,您需要使用延迟扩展,因此请在批处理文件的开头添加以下内容:

if exist "C:\Windows\System32\updatevmcheck.txt" (
set /p Build=<C:\Windows\System32\updatevm.txt
if %Build% LSS 2  (
echo "Run Code Here"
) else (
exit
)
echo 2 > C:\Windows\System32\updatevmcheck.txt
exit
) 
ELSE (
echo 1 > C:\Windows\System32\updatevmcheck.txt
exit
)

然后使用
!建造而不是
%Build%
。有关更多详细信息和解释,请参见
help set
(我已经写了几十遍了;)。

请注意,除非您打算退出命令处理器,否则不应在批处理文件中调用
exit
,例如在交互式会话中。要退出批处理文件,请使用
exit/b
goto:eof
setlocal enabledelayedexpansion