Read a file into a variable
Posted: 14 Aug 2011 13:16
This batchfile will read itself and store all it's lines including crlf in a variable.
It uses a variation on the 'set /p can read multiple lines from a file' trick mentioned by Jeb.
The core snippet
The full code with test display, redirect to file and TODO's
File2Var.bat
Code only tested on Windows Vista.
OJB
It uses a variation on the 'set /p can read multiple lines from a file' trick mentioned by Jeb.
The core snippet
Code: Select all
echo The batchfile will be read into %%File%%
set "ShowLineNr="
set "File="
(
for /l %%a in (1,1,%LineCount%) do (
set line=
set /p line=
if defined ShowLineNr set LineNr=[%%a]
set File=!File!!LineNr!!line!
if %%a LSS %LineCount% set File=!File!^!CR!^!LF!
)
)<%~nx0
The full code with test display, redirect to file and TODO's
File2Var.bat
Code: Select all
@echo off
setlocal EnableDelayedExpansion
cls
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set LF=^
rem TWO Empty lines are neccessary
set/a LineCountTypeFind=0
set/a LineCountFind=0
set/a LineCountFindStr=0
rem get number of lines using type find
for /f %%a in ('type "%~nx0"^|find "" /V /C') do set /a LineCountTypeFind=%%a
echo LineCountTypeFind=%LineCountTypeFind%
rem get number of lines using find
for /f "tokens=2 delims=:" %%a in ('find /v /c "" "%~nx0"') do set/a LineCountFind=%%a
echo LineCountFind=%LineCountFind%
rem get number of lines using findstr
for /f "delims=:" %%a in ('findstr /R /N "^" "%~nx0"') do set/a LineCountFindStr=%%a
echo LineCountFindStr=%LineCountFindStr%
set/a LineCount=%LineCountFindStr%
echo LineCount=%LineCountFindStr%
echo The batchfile will be read into %%File%%
echo To include linenumbers add a value to the var ShowLineNr on the next line
echo This will also suppress the filecompare at the end
pause
set "ShowLineNr="
set "File="
(
for /l %%a in (1,1,%LineCount%) do (
set line=
set /p line=
if defined ShowLineNr set LineNr=[%%a]
set File=!File!!LineNr!!line!
if %%a LSS %LineCount% set File=!File!^!CR!^!LF!
)
)<%~nx0
echo The file is loaded into %%File%% let's try to view this
echo .
echo ... begin view by rem
echo on
rem %File%
@echo off
echo ... end view by rem
echo .
echo So rem show only the first line
echo .
pause
rem add blank line prefix so view by set looks better
set File=^!CR!^!LF!!File!
echo .
echo ... begin view by set
set File
echo ... end view by set
echo .
echo YES, blank lines, linefeeds everythings seems to be there
echo .
pause
>nul copy nul "%~n0-Set.txt"
echo ... begin redirect set to file
>>"%~n0-Set.txt" set File
echo ... end redirect set to file
echo .
echo ... begin Type redirected file
type "%~n0-Set.txt"
echo ... end redirected file
pause
echo .
echo Not happy with that pesky first line 'File='
echo OK so we get rid of that line
echo .
more +1 "%~n0-Set.txt" > "%~n0-Set.bat"
echo .
echo ... begin Type new batchfile
type "%~n0-Set.bat"
echo ... end Type new batchfile
echo .
if not defined ShowLineNr (
echo compare redirected file with the original batchfile
echo .
fc "%~nx0" "%~n0-Set.txt"
echo .
echo Just one difference, the first line with File=
echo .
echo compare new batchfile with the original batchfile
echo .
fc "%~nx0" "%~n0-Set.bat"
echo .
echo Yeah, no difference^!
echo .
pause
)
echo .
echo TODO view by echo TODO
echo .
echo TODO test if linecount is reliable
echo TODO test if content can break it
echo .
endlocal
pause
Code only tested on Windows Vista.
OJB