1.

Solve : Load text file strings into variables?

Answer»

I'm running XP with SP 2. Command prompt version shows [Version 5.1.2600].

As part of a DOS batch job I'm developing, I'm trying to read 8 lines from a text file and want to store the string on each line as a separate variable for later use within the script. Variable names could be as simple as Var1, Var2, Var3, etc.

I've been able to read the lines and echo them to the screen via a FOR loop, and planned to somehow make use of a numeric COUNTER to populate the variables. Haven't quite determined the method -- possibly by appending the counter to the variable names, or possibly by calling the FOR loop once for each line read, but jumping out of the loop after skipping through the appropriate number of lines each time.

My counter seems to get incremented correctly as it shows "8" if I echo the value after the loop ENDS, but I haven't been able to echo the correct value within the FOR loop, nor have I been able to reference the current value within an IF statement inside the loop.


Here's the code I'm trying:

Set Count=0
For /F "tokens=*" %%A in (test_input.txt) Do (
Set Var=%%A
Set /A Count+=1
Echo %%A
Echo %Var%
Echo %Count%
)
Echo %Count%


Here are the contents of test_input.txt:

One
Two
Three
Four
Five
Six
Seven
Eight


Here's the output I'm getting:

One
Eight
0
Two
Eight
0
Three
Eight
0
Four
Eight
0
Five
Eight
0
Six
Eight
0
Seven
Eight
0
Eight
Eight
0
8


The "8" at the bottom seems to show the correct Count value after the loop has ended, but the Count is ALWAYS showing "0" within the loop.

I'm also not sure why I'm getting "Eight" for the Var variable each time around. I'd appreciate an explanation if anyone can offer one.

Also, in doing research, I've seen people sometimes using exclamation points around variables, such as !Count!, but I'm not sure of their significance. Could someone please clue me in on their relevance?

Thanks in advance.
what exactly are you trying to do for your project? post a sample of input, your expected output, and the code you have so far. (and any other information)The snippet of code and sample input file I'm CURRENTLY testing is above. For that, I was expecting to get output along the lines of the following:

One
One
1
Two
Two
2
Three
Three
3
Four
Four
4
Five
Five
5
Six
Six
6
Seven
Seven
7
Eight
Eight
8
8


What I'm really trying to achieve is to load some variables with values related to the environment being used. Those values are in the text file being read. (The "echo" lines in my original code above are just for debugging.)

What I was hoping to do was something like:

Set Count=0
For /F "tokens=*" %%A in (test_input.txt) Do (
Set /A Count+=1
Set Var%Count%=%%A
)

My intent was to set variables of Var1=One, Var2=Two, Var3=Three, etc. (The actual values in the file are logins, passwords, filenames, paths, etc.)
You experience your problem because ordinary percent sign variables within a loop are EVALUATED once only, before the loop is executed.

You need to use delayed expansion and also use the exclamation mark as a variable delimiter instead of a percent sign within the loop.

Also, Var%count% won't work. You cannot make a variable name out of a variable value!







you have misunderstood what i am asking.
show what your test_input.txt looks like, and after manipulation, what are you expecting to see as output. That way, ppl here may suggest better ways of doing it instead of what you are doing now. The delayed expansion with exclamation points used for variable delimiters within the loop did the trick. I'm now able to load environment variables from a text file. Thanks for the tips!

I'll attach sample code for what I've got running. I basically have a main process which calls a routine called SET_VARIABLE once for each line in the input file, test_input.txt, and then echos the values of all 8 variables at the end. (In the batch file that I'll be deploying, the environment variables will have more descriptive names, and the input file will have values other than simply the {One, Two, ... Eight} I've been testing with as shown in my original post.)

The output for the code below looks like:

VAR_1=One
VAR_2=Two
VAR_3=Three
VAR_4=Four
VAR_5=Five
VAR_6=Six
VAR_7=Seven
VAR_8=Eight


Here's the tested code:

@Echo Off
setlocal enabledelayedexpansion
goto MAIN

rem --------------------------
rem Procedure Set_Variable

:SET_VARIABLE

Set Count=0
For /F "tokens=*" %%A in (test_input.txt) Do (
Set Var=%%A
Set /A Count+=1
if !Count!==%INI_FILE_POSITION% goto DONE_FOR_LOOP
)
:DONE_FOR_LOOP
set !VARIABLE_TO_SET!=%Var%

if %INI_FILE_POSITION%==1 goto DONE_SET_VARIABLE_1
if %INI_FILE_POSITION%==2 goto DONE_SET_VARIABLE_2
if %INI_FILE_POSITION%==3 goto DONE_SET_VARIABLE_3
if %INI_FILE_POSITION%==4 goto DONE_SET_VARIABLE_4
if %INI_FILE_POSITION%==5 goto DONE_SET_VARIABLE_5
if %INI_FILE_POSITION%==6 goto DONE_SET_VARIABLE_6
if %INI_FILE_POSITION%==7 goto DONE_SET_VARIABLE_7
if %INI_FILE_POSITION%==8 goto DONE_SET_VARIABLE_8
echo Error -- INI_FILE_POSITION %INI_FILE_POSITION% not recognized
goto END


rem --------------------------
rem Main Process

:MAIN

set VARIABLE_TO_SET=VAR_1
set INI_FILE_POSITION=1
goto SET_VARIABLE
:DONE_SET_VARIABLE_1
set VARIABLE_TO_SET=VAR_2
set INI_FILE_POSITION=2
goto SET_VARIABLE
:DONE_SET_VARIABLE_2
set VARIABLE_TO_SET=VAR_3
set INI_FILE_POSITION=3
goto SET_VARIABLE
:DONE_SET_VARIABLE_3
set VARIABLE_TO_SET=VAR_4
set INI_FILE_POSITION=4
goto SET_VARIABLE
:DONE_SET_VARIABLE_4
set VARIABLE_TO_SET=VAR_5
set INI_FILE_POSITION=5
goto SET_VARIABLE
:DONE_SET_VARIABLE_5
set VARIABLE_TO_SET=VAR_6
set INI_FILE_POSITION=6
goto SET_VARIABLE
:DONE_SET_VARIABLE_6
set VARIABLE_TO_SET=VAR_7
set INI_FILE_POSITION=7
goto SET_VARIABLE
:DONE_SET_VARIABLE_7
set VARIABLE_TO_SET=VAR_8
set INI_FILE_POSITION=8
goto SET_VARIABLE
:DONE_SET_VARIABLE_8

Echo VAR_1=%VAR_1%
Echo VAR_2=%VAR_2%
Echo VAR_3=%VAR_3%
Echo VAR_4=%VAR_4%
Echo VAR_5=%VAR_5%
Echo VAR_6=%VAR_6%
Echo VAR_7=%VAR_7%
Echo VAR_8=%VAR_8%

:END

what you need is arrays...
Code: [Select]Set objFSO=CreateObject("Scripting.FileSystemObject")
Set myFile=objFSO.OpenTextFile("c:\temp\test.txt")
Do until myFile.AtEndOfStream
linenumber=myFile.Line
If linenumber = 9 Then
Exit Do
End If
ReDim Preserve myArray(linenumber)
myArray(linenumber)=myFile.ReadLine
Loop
For i=LBound(myArray) To UBound(myArray)
WScript.Echo myArray(i)
Next
I had a question on something like this but didn't really know how to phrase it... this really helped me a lot though.



Discussion

No Comment Found