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!

Rakha
  • 1,874
  • 3
  • 26
  • 60

1 Answers1

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%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Perfect! I tried many things with delayedexpansion and !! but i was forgetting the iterator! Much appreciated. – Rakha Jul 12 '17 at 15:42
  • _Iterator_? I think the [array terminology](https://en.wikipedia.org/wiki/Array_data_structure) call this part _index_ or _subscript_. See [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). – Aacini Jul 12 '17 at 19:03