1

I have this temp2.txt file :

name              : XXX1
name              : XXX2

I want to have each word after ": " saved to separate variables so i can process them further with commands.

I'm trying :

for /f "tokens=1,2 delims=: " %%a in (C:\temp\temp2.txt) do (
set userloop1=%%b
set userloop2=%%b
)


echo %userloop1%
echo %userloop2%

pause

the loop works but each variable contains the last occurence of the loop, which is XXX2.

I want to have userloop1 returning XXX1 and userloop2 retuning XXX2. How do i adjust the loop to have this working?

Thank you so much!

1 Answer 1

1

Use an iterator under delayed expansion:

setlocal enableDelayedExpansion
set iterator=1
for /f "tokens=1,2 delims=: " %%a in (C:\temp\temp2.txt) do (
  set userloop!iterator!=%%b
  set /a iterator=iterator+1
)


echo %userloop1%
echo %userloop2%
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! I tried many things with delayedexpansion and !! but i was forgetting the iterator! Much appreciated.
Iterator? I think the array terminology call this part index or subscript. See this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.