user94945
user94945

Reputation: 31

Print array element value in a Batch file

There is a problem with printing array element values in the below code:

@echo off 
setlocal enabledelayedexpansion enableextensions
for /F "tokens=2,3 delims= " %%a in ('findstr "associationMaxRtx maxIncomingStream maxOutgoingStream initialAdRecWin maxUserdataSize mBuffer nThreshold PathMaxRtx maxInitialRtrAtt minimumRto maximumRto initialRto rtoAlphaIndex tSack" C:\Users\ephajin\logs.txt') do (
     set /A count+=1
     set vartmp1=%%a
     set vartmp2=%%b
     set "array[!count!]="%%a %%b""
)



(for /L %%i in (1,1,%count%) do  echo !array[%%i]!
) > result.txt

in the result file I get the output

ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.

It does not print the array values.

The problem is probably due to setlocal enabledelayedexpansion but how do you correct it?

Upvotes: 2

Views: 1444

Answers (1)

Magoo
Magoo

Reputation: 80033

FOR /L %%a IN (1,1,4) DO ECHO !array[%%a]!

FOR /f "tokens=1*delims==" %%a  IN ('set array[') DO ECHO %%b

Either of these two lines should show you what you appear to require.

Since the first is identical in effect to your code, I suspect that the array[*] array isn't being established correctly. you can check this by executing

set array[

to show precisely what has been set. Actually,

set

should show you all defined user-variables.

set|more

would show the same, but allow you to page through them.

SET "result="
FOR /f "tokens=1*delims==" %%a  IN ('set array[') DO SET "result=!result! %%b"
ECHO result: "%result%" or "%result:~1%"
echo===============
SET "result="
FOR /L %%a IN (1,1,4) DO SET "result=!result! !array[%%a]!"
ECHO result: "%result%" or "%result:~1%"

Two methods of setting result - the list of the values in the array. Naturally, the space in the set instruction could be almost any character you desire - comma, for instance. The result is shown both with a leading space and with that space removed.

Upvotes: 1

Related Questions