Nested for loops to seperate data

June 17, 2009 at 09:31:45
Specs: Windows XP
I have a file that has 10 strings, each separated by commas (no returns).  I am trying to get each of the separate strings (between the commas) to be assigned to a seperate variable labeled string.1,string.2,string.3, and so forth.  I used a for loop with 10 tokens and setting the delimiters to a comma.  I used the variable %%1, so that the different values are stored to %%1,%%2,%%3, etc.  Rather than go through the process of doing something like:

set String.1=%%1
set String.2=%%2
set String.3=%%3

etc., I figured it would be easier to have another for loop inside. This is what I have so far:

for /f "tokens=1,2,3,4,5,6,7,8,9,10 delims=," %%1 in (Strings.txt) do (
for /l %%X in (1,1,10) do (
set String.%%X=%%%%X

The idea is that it will set String.1 equal to %%1, and do the same with all the others. However, instead, it sets String.1's value to "%1", and not the first string from the file. If I tell String.1 to equal %%1 and not %%%%X, even within the other for loop, it works, so the problem is not that the variables from the outer for loop do not work in the inner one.

Can anyone tell me what I could do about this? I realize that I could probably do this more easily by just having String.1=%%1, String.2=%%2, etc. but I might decide to make the list longer, and if I do, then it will just be too long.

Another easier solution that may not work would be to store the entire text file to a variable. If there is a function to find the position of a character in a variable, then then I could use that to split the variable into different parts by finding commas. I do not know if there is one, however, but if there is, could someone please tell me what the syntax is?

See More: Nested for loops to seperate data

Report •

June 17, 2009 at 13:24:02
%1 (the script's first command line argument) is not the same thing as %%1 (literally a percent sign followed by a "1"). Are you sure you want the latter?

Report •

June 17, 2009 at 13:32:20
@echo off & setlocal EnableDelayedExpansion
set /P myFile=< Strings.txt
set cnt=0
set myFile=%myFile: =@@%
for %%j in (%myFile%) do (
  set /A cnt+=1  
  set item=%%j  
  set string.!cnt!=!item:@@= !

You need to use Dynamic Variables marked by ! and in your original script you can't choose %%1 as the starting variable as there is no %%10 identifier.

Report •

June 17, 2009 at 13:32:35
I want to have the value of %1,%2, etc., but in a for loop, you have to put a % first, so it looks like %%1.

Report •

Related Solutions

June 17, 2009 at 13:49:47
Actually, it's not the FOR loop that demands the extra percent sign. It's the script parser that runs before the FOR loop. It's this parser that fills in the command line arguments, environment variables, and combines %% into %. It's a subtle but important distinction.

set cnt=1
set String.%cnt%=%1
set /a cnt += 1
if not "%~1"=="" goto loop

Report •

June 18, 2009 at 06:47:41
Oh, ok, I didn't know that. Thanks for clarifying.

Report •

Ask Question