To read content from a file, we set a variable using /P and the < sign after the usual = sign.
The following code does this.
@echo off
:init_vars
set /P file_content=<file1.txt
goto action
:action
echo File content: %file_content%
goto eof
:eof
echo Press any key to close window...
pause > nul
goto blackhole
:blackhole
Note that if file1.txt doesn't exist an error message will show and the variable %file_content% will be empty.
Hey presto, we got something out of the file. This is great... except: we are only getting hold of the first line.
In the two following posts, I will explain the loop construct and how to use it to read all the lines from the file.
Thoughts in the meantime?
No comments:
Post a Comment