variable and text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

variable and text file

#1 Post by Mohammad_Dos » 27 Jan 2011 05:47

how to save the text in a text file into a variable as input?


how to set output of a command as a variable?

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: variable and text file

#2 Post by ChickenSoup » 27 Jan 2011 08:47

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: variable and text file

#3 Post by aGerman » 28 Jan 2011 13:14

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

Regards
aGerman

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: variable and text file

#4 Post by Mohammad_Dos » 18 Feb 2011 12:47

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

Post Reply