Computing.Net > Forums > Programming > Nested for loops to seperate data

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Nested for loops to seperate data

Reply to Message Icon

Name: gaga654
Date: June 17, 2009 at 09:31:45 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: June 17, 2009 at 13:24:02 Pacific
Reply:

%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?


0

Response Number 2
Name: IVO
Date: June 17, 2009 at 13:32:20 Pacific
Reply:

@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.

0

Response Number 3
Name: gaga654
Date: June 17, 2009 at 13:32:35 Pacific
Reply:

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.


0

Response Number 4
Name: Razor2.3
Date: June 17, 2009 at 13:49:47 Pacific
Reply:

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
:loop
set String.%cnt%=%1
shift
set /a cnt += 1
if not "%~1"=="" goto loop


0

Response Number 5
Name: gaga654
Date: June 18, 2009 at 06:47:41 Pacific
Reply:

Oh, ok, I didn't know that. Thanks for clarifying.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Rename file if it has spa... need to install win xp



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Nested for loops to seperate data

using for loops to interate unicode www.computing.net/answers/programming/using-for-loops-to-interate-unicode/12858.html

Nested FOR loop prevents parent from complete www.computing.net/answers/programming/nested-for-loop-prevents-parent-from-complete/20042.html

Batch file nested FOR loops help www.computing.net/answers/programming/batch-file-nested-for-loops-help/15196.html