Line read in a loop from a file doesn't assign properly in batch program

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jrahman
Posts: 2
Joined: 18 Jun 2018 10:15

Line read in a loop from a file doesn't assign properly in batch program

#1 Post by jrahman » 18 Jun 2018 10:21

I am reading each line of a .txt file in a .bat program using For /f loop. The .txt file contains all the names of the test cases (e.g. ShipmentPostErrorPathXPERF.txt) which I need to read and assign to a variable called testCaseName and pass this to another .cmd program as a parameter and continue in loop until it reads all lines (test cases),

Here is my sample batch file, my problem is when I assign testCaseName=%%A, it only shows assigned to the last line of the file (REST0007-SHIPMETDATA_POST-XPERF_ErrorPath-ShipmentNULLServiceSchedID_TestCase). But when I use echo %%A, it shows all lines as it reads in loop.

Why set testCaseName=%%A in the batch file only reading the last line ?

.batch file:

Code: Select all

echo off
cd d:\SWAT
set fileNameXPERF=d:\SWAT\ShipmentPostErrorPathXPERF.txt
for /F "tokens=*" %%A in (D:\SWAT\ShipmentPostErrorPathXPERFxxx.txt) do (
set testCaseName=%%A
echo %testCaseName%
echo %%A
call d:\SWAT\testRunner.cmd %testCaseName%
)
The text file (ShipmentPostErrorPathXPERF.txt):

Code: Select all

REST0007-SHIPMETDATA_POST-XPERF_ErrorPath-ShipmentEmptyDockNum_TestCase
REST0007-SHIPMETDATA_POST-XPERF_ErrorPath-ShipmentNULLDockNum_TestCase
REST0007-SHIPMETDATA_POST-XPERF_ErrorPath-ShipmentInvalidDockNum_TestCase
REST0007-SHIPMETDATA_POST-XPERF_ErrorPath-ShipmentEmptyServiceSchedID_TestCase
REST0007-SHIPMETDATA_POST-XPERF_ErrorPath-ShipmentNULLServiceSchedID_TestCase

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Line read in a loop from a file doesn't assign properly in batch program

#2 Post by jeb » 18 Jun 2018 11:32

Hi jrahman,

I closed your question on StackOverlflow as it is a duplicate of Variables in batch not behaving as expected.
There is explained why your echo %testCaseName% fails.
Percent expansion is evaluated when a block is parsed, before it will be executed.
So in your sampe %testCaseName% is expanded to the value it has BEFORE you enter the loop.
It only seems that it shows always the last value, because after your first run in testCaseName is the last line.

The solution (see the answers in the duplicate question) is delayed expansion.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

cd d:\SWAT
set fileNameXPERF=d:\SWAT\ShipmentPostErrorPathXPERF.txt
for /F "tokens=*" %%A in (D:\SWAT\ShipmentPostErrorPathXPERFxxx.txt) do (
  set "testCaseName=%%A"
  echo #1 !testCaseName!
  echo #2 %%A
  call d:\SWAT\testRunner.cmd !testCaseName!
)
Hope it helps
jeb

jrahman
Posts: 2
Joined: 18 Jun 2018 10:15

Re: Line read in a loop from a file doesn't assign properly in batch program

#3 Post by jrahman » 18 Jun 2018 14:35

Thank jeb.
It works !
You saved my day, thanks a lot !

/jamil

Post Reply