how to save the text in a text file into a variable as input?
how to set output of a command as a variable?
variable and text file
Moderator: DosItHelp
-
- Posts: 84
- Joined: 08 Sep 2010 10:25
- Location: Iran,Kashan
- Contact:
-
- Posts: 79
- Joined: 13 Dec 2010 10:32
Re: variable and text file
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:
Output:
File test.txt:
Output:
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.
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
There is a small trick to get the first line of a text file.
Regards
aGerman
set /p "testvar="<"test.txt"
echo %testvar%
Regards
aGerman
-
- Posts: 84
- Joined: 08 Sep 2010 10:25
- Location: Iran,Kashan
- Contact:
Re: variable and text file
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