Reputation: 9836
I need to make a batch file that can read a text file and I am using windows 7 ultimate 32 bit
I am currently using this code:
@ECHO OFF
for /f "delims=" %%x in (test.txt) do set "Var=%%x"
ECHO %Var%
pause
But this only reads the first line and I need it to read the whole thing. I need it to display the full text file.
Upvotes: 0
Views: 194
Reputation: 1901
Try this:
@ECHO OFF
SetLocal EnableDelayedExpansion
for /f "delims=" %%x in ('type test.txt') do (
set "Var=%%x"
ECHO !Var!
)
pause
You need to enclose the for
loop with brackets if you are performing multiple commands inside the for
loop. Besides that, SetLocal EnableDelayedExpansion
will helps to expand the variable at execution time rather than at parse time.
Hope this helps.
Upvotes: 3
Reputation: 354864
Actually, this should read the whole file and set Var
to the last line.
If you need to process the whole file, you have several options:
If you just need to do something for every line, then just use a block instead of the set "Var=%%x"
:
for /f "delims=" %%x in (test.txt) do (
rem Do something with %%x
)
If you need the complete file line-by-line in memory, use a counter and emulate arrays with lots of variables:
setlocal enabledelayedexpansion
set cnt=0
for /f "delims=" %%x in (test.txt) do (
set "ine[!cnt!]=%%x"
set /a cnt+=1
)
set /a numlines=cnt-1
and afterwards you can just use a for /l
loop to access them again.
Upvotes: 2