Read a file into a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Read a file into a variable

#1 Post by OJBakker » 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

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Read a file into a variable

#2 Post by jeb » 14 Aug 2011 15:47

Hi OJBakker,

nice post :)
And it shows the possibilities of the "multiple set /p reading"

Some hints
set File=!File!^!CR!^!LF!
The carets are useless, as they are removed before the delayed expansion begins.

OJBakker wrote:rem %File%
@echo off
echo ... end view by rem
echo .
echo So rem show only the first line

It's the expected result, as an unmasked linefeed stops the batch parser.

Your solution with the "set file" works, but you have to remove the first line.
It's not necessary, you could solve it with

Code: Select all

echo !file!


jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Read a file into a variable

#3 Post by dbenham » 15 Aug 2011 07:57

OJBakker wrote:

Code: Select all

...
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%
...

Are you seeing differences in the output of the different techniques for determining the number of lines in a file :?:

The only differences I find are in performance:
1) find - fastest, but is not convenient if file name may or may not include drive letter.
2) type | find - a bit slower because pipe requires extra instantiation of CMD.EXE.
3) findstr - much slower because each line is passed to loop, last line "wins"
4) type| findstr (not shown) - slowest

My favorite is 2) because it works both with and without the Drive letter in the file name.

1) can be made more flexible like so:

Code: Select all

rem get number of lines using find - with or without drive letter in name
for /f "delims=" %%a in ('find /V /C "" "%~f0"') do set LineCountFind=%%a
set LineCountFind=%LineCountFind:*: =%
echo LineCountFind=%LineCountFind%

I still prefer 2) because it is simpler and only slightly slower

Dave Benham

susanxyparke
Posts: 1
Joined: 15 Aug 2011 22:28

Re: Read a file into a variable

#4 Post by susanxyparke » 15 Aug 2011 22:39

The most simple way to access these files is to use your CD or DVD Burner to burn them to a disk. Nero is a common program that is used to burn bin/cue files to hard media.
Force Factor

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Read a file into a variable

#5 Post by jeb » 16 Aug 2011 02:31

And there is one more functionality ...
It can read even lines when they are appended later (while reading).
Moved to New technic: set /p can read multiple lines from a file

Post Reply