Brian T Hannan
Brian T Hannan

Reputation: 4005

Why does my windows batch file keep looping after the condition is met?

Basically, I want to copy the latest version of an MSI from a server to my local machine. I am trying to loop through a file and grab the first line which holds the latest version of the MSI to grab. I am not entirely familiar with the weirdness of for loops and if statements in batch files. Here is the code I have that keeps looping after it finds the first line:

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

Upvotes: 2

Views: 440

Answers (1)

Andriy M
Andriy M

Reputation: 77677

In this line:

if "counter"==1 goto install (

"counter" can never be equal to 1. On the other hand, !counter! might.

Explanation (in case you need it):

"counter" is a literal, a word counter in double quotes. You are comparing it to another literal, 1. Obviously the two don't match. What is most probably meant in that part of the script is evaluating the variable counter and comparing the value with 1. In bracketed command blocks one typically uses delayed expansion, hence !counter! (as opposed to %counter%).

On a different note, the said line seems somewhat unusual. It contains the goto command and another command after it. I don't think that the command that follows goto is likely to be executed. Possibly goto install is redundant.

Upvotes: 3

Related Questions