how to store output of file into variable

January 3, 2011 at 03:46:24
Specs: Windows 2003 server
i want to store output of text file into a variable. So i can pass the whole file as parameter.

I am using windows 2003 server

The text files having multiple lines like
10.20.210.100 fish
10.20.210.101 rock

I was using
Set /P var=<file.txt

it reads only 1st line of the file. So if file having only 1 line then it’s ok .

I tried with FOR LOOP also but it assigning the last value to variable.

Need help


See More: how to store output of file into variable

Report •


#1
January 3, 2011 at 03:53:41
You can't achieve that anyway since an environment variable is a string ended by a CR/LF. So it is not possible to store into one variable a multi-lines text file.


Report •

#2
January 3, 2011 at 03:55:53
Do you want eaxh line in a separate var or all the lines strung together?


=====================================
Life is too important to be taken seriously.

M2


Report •

#3
January 3, 2011 at 03:59:24
all the lines strung together...

Report •

Related Solutions

#4
January 3, 2011 at 23:26:51
I got the Ans..

@echo off

SETLOCAL EnableDelayedExpansion

set "line="

for /f "delims=" %%a IN (example.txt) do (

set "line1=%%a"

set "line=!line1! and %%a"

)

echo !line!

or

echo %line%


Report •

#5
March 21, 2011 at 22:27:15
can this be done for a command output as well ..
I tried the following code :-
@echo off

SETLOCAL EnableDelayedExpansion

set "line="

for /f "delims=" %%a IN ('net use \\%computername%\C$ /delete /y') do (

set "line1=%%a"

set "line=!line1! and %%a"

)

echo error = !line!

But it didn't worked .. When I executed this batch file output was :-
The network connection could not be found.

More help is available by typing NET HELPMSG 2250.

error =

last line was error = thats it!!.. I exepected it to be the above error message..
Could you help me here .. Thanks !


Report •

Ask Question