Page 1 of 1

variable and text file

Posted: 27 Jan 2011 05:47
by Mohammad_Dos
how to save the text in a text file into a variable as input?


how to set output of a command as a variable?

Re: variable and text file

Posted: 27 Jan 2011 08:47
by ChickenSoup
Explore the use of "FOR /F" for these. Anything over one line will have to go to multiple variables (as far as i know).

Examples:

Code: Select all

for /f "tokens=*" %%a in ('echo.I like turtles') do set testvar=%%a
echo.%testvar%

Output:

Code: Select all

I like turtles


File test.txt:

Code: Select all

this is a test on line 1.
this is a test on line 2.

Code: Select all

for /f "tokens=*" %%a in (test.txt) do set testvar=%%a
echo.%testvar%

Output:

Code: Select all

this is a test on line 2.

This FOR will set the final line in the file as a variable. Generally you would search the file for the line you want versus setting each line to a variable.

Re: variable and text file

Posted: 28 Jan 2011 13:14
by aGerman
There is a small trick to get the first line of a text file.
set /p "testvar="<"test.txt"
echo %testvar%

Regards
aGerman

Re: variable and text file

Posted: 18 Feb 2011 12:47
by Mohammad_Dos
aGerman wrote:There is a small trick to get the first line of a text file.
set /p "testvar="<"test.txt"
echo %testvar%

Regards
aGerman

aGerman wrote:There is a small trick to get the first line of a text file.
set /p "testvar="<"test.txt"
echo %testvar%

Regards
aGerman

thank u
interesting