为什么我的Windows批处理文件在满足条件后会保持循环?

时间:2011-07-13 16:25:20

标签: windows batch-file if-statement for-loop

基本上,我想将最新版本的MSI从服务器复制到本地计算机。我试图循环一个文件并抓住第一行,其中包含最新版本的MSI来抓取。我并不完全熟悉批处理文件中for循环和if语句的奇怪之处。这是我找到第一行后继续循环的代码:

cd %~dp0

mkdir "c:\MyApp\QA\msi"

rem Determine what folder is the latest version of QA
setlocal enabledelayedexpansion
dir /b /o-n "\\my-server\folder\another_folder\5.0.*" > output.txt
SET /a counter=1
SET version=""
for /f "usebackq delims=" %%a in (output.txt) do (
if "counter"==1 goto install (
xcopy "\\my-server\folder\another_folder\%%a\myinstaller.msi" "c:\MyApp\QA\msi\myinstaller.msi" /y
)
SET /a counter+=1
)

goto exit

:exit

PAUSE

1 个答案:

答案 0 :(得分:3)

在这一行:

if "counter"==1 goto install (

"counter" 永远不会等于1。另一方面, !counter! 可能会。

解释(如果您需要):

"counter"是双引号中的文字,单词计数器。您正在将其与另一个文字1进行比较。显然两者不匹配。脚本的这一部分最有可能意味着评估变量 counter并将值与1进行比较。在括号中的命令块中,通常使用延迟扩展,因此!counter!(而不是%counter%)。

另一方面,上述线似乎有点不寻常。它包含goto命令和之后的另一个命令。我不认为goto之后的命令可能会被执行。可能goto install是多余的。