Unable to set correct parameters in for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Birage
Posts: 7
Joined: 17 Feb 2019 08:32

Unable to set correct parameters in for loop

#1 Post by Birage » 18 Feb 2019 07:51

cmd has multiline output, and I am trying to extract something out of each line by using tokens.

I assigned each line to a repeating var1,var2 and so on, in this line set var!count!=%%H.

However, I am unable to code this for loop for /f "tokens=1" %%S in (!tempvar!) do ( to properly accept the correct value of var1 (var2..var3..).

echoing !var1! will output the first line of cmd's output correctly. Hence, I also tried !var!count2!! and var!count2! as parameters but it doesn't work.

Any help is appreciated, cheers :)

Code: Select all

@echo off
setlocal enabledelayedexpansion

set /a count=0
set cmd="command-to-be-run"
for /f "delims=" %%H in ('!cmd!') do (
	set /a count+=1
	set var!count!=%%H
)

set /a count2=0
for /L %%Z in (1,1,!count!) do (
	set /a count2+=1
	set tempvar=var!count2!
	
	for /f "tokens=1" %%S in (!tempvar!) do (
	echo do something here
	)
)


Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Unable to set correct parameters in for loop

#2 Post by Squashman » 18 Feb 2019 09:00

There is absolutely no need for the count2 variable. You can just use the FOR loop variable.
Currently you are just assigning a string to the variable. I assume you want the value of the array variable. No need to assign it to another environmental variable either. Just use it directly in your next command.

Code: Select all

for /L %%Z in (1,1,!count!) do (
	for /f "tokens=1" %%S in ("!var%%Z!") do (
	     echo do something here
	)
)

Post Reply