105

How you can read a file (text or binary) from a batch file? There is a way to read it in a binary mode or text mode?

Lucas Gabriel Sánchez
  • 34,344
  • 20
  • 53
  • 81

7 Answers7

72

Under NT-style cmd.exe, you can loop through the lines of a text file with

FOR /F %i IN (file.txt) DO @echo %i

Type "help for" on the command prompt for more information. (don't know if that works in whatever "DOS" you are using)

devio
  • 35,442
  • 6
  • 73
  • 138
  • 34
    This command would quit reading a line if it found a whitespace character. I eventually ended up using FOR /F "tokens=*" %%i IN (file.txt) DO @ECHO %%i – Jason Mar 27 '12 at 18:07
  • 10
    It works! and If you are using console try %i if you are using a bat file then try %%i . – Steven Du Mar 17 '13 at 12:55
43

The FOR-LOOP generally works, but there are some issues. The FOR doesn't accept empty lines and lines with more than ~8190 are problematic. The expansion works only reliable, if the delayed expansion is disabled.

Detection of CR/LF versus single LF seems also a little bit complicated.
Also NUL characters are problematic, as a FOR-Loop immediatly cancels the reading.

Direct binary reading seems therefore nearly impossible.

The problem with empty lines can be solved with a trick. Prefix each line with a line number, using the findstr command, and after reading, remove the prefix.

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    echo(!var!
    ENDLOCAL
)

Toggling between enable and disabled delayed expansion is neccessary for the safe working with strings, like ! or ^^^xy!z.
That's because the line set "var=%%a" is only safe with DisabledDelayedExpansion, else exclamation marks are removed and the carets are used as (secondary) escape characters and they are removed too.
But using the variable var is only safe with EnabledDelayedExpansion, as even a call %%var%% will fail with content like "&"&.

EDIT: Added set/p variant
There is a second way of reading a file with set /p, the only disadvantages are that it is limited to ~1024 characters per line and it removes control characters at the line end.
But the advantage is, you didn't need the delayed toggling and it's easier to store values in variables

@echo off
setlocal EnableDelayedExpansion
set "file=%~1"

for /f "delims=" %%n in ('find /c /v "" %file%') do set "len=%%n"
set "len=!len:*: =!"

<%file% (
  for /l %%l in (1 1 !len!) do (
    set "line="
    set /p "line="
    echo(!line!
  )
)

For reading it "binary" into a hex-representation
You could look at SO: converting a binary file to HEX representation using batch file

Community
  • 1
  • 1
jeb
  • 70,992
  • 15
  • 159
  • 202
  • Thanks for the pointer on the use of `set /p`. I used `set /p value=< result.txt` get the first line from a text file – SteveC May 04 '12 at 09:45
  • Jeb, I asked a related question based on your solution above in http://stackoverflow.com/questions/11408295/with-dos-batch-file-how-to-parse-a-file-and-output-part-to-one-file-part-to-an – Nicholas Jul 10 '12 at 07:15
  • What about the EOF (end-of-file) character, ASCII `0x1A`? does `for /F` behave the same way as with NUL (`0x00`) characters? – aschipfl Sep 01 '15 at 21:34
  • @aschipfl I suppose the 0x1A behaves in some cases like a linefeed (or better it is translated to a linefeed). [Is there a way to create a SUB without using a temp file ?](http://www.dostips.com/forum/viewtopic.php?f=3&t=2863&start=15) – jeb Sep 03 '15 at 06:23
  • 1
    this should be the accepted answer, it's foolproof even if the lines start with `:` or `;` or contain `!` – HaxAddict1337 Jul 21 '20 at 14:53
40

You can use the for command:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

Type

for /?

at the command prompt. Also, you can parse ini files!

johnstok
  • 87,006
  • 12
  • 51
  • 76
  • 31
    This is a pretty complex example, parsing very specific information from a particularly formatted text file, without giving any explanation. As per `for /?` the command *would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces.* – Martin Prikryl Dec 22 '14 at 13:37
  • 3
    Your example is from cmd.exe's help. You should add a hint for that or copy the whole description. – Th. Thielemann Sep 25 '19 at 10:54
13

One very easy way to do it is use the following command:

set /p mytextfile=< %pathtotextfile%\textfile.txt
echo %mytextfile%

This will only display the first line of text in a text file. The other way you can do it is use the following command:

type %pathtotextfile%\textfile.txt

This will put all the data in the text file on the screen. Hope this helps!

J. Bond
  • 396
  • 3
  • 7
1

Well theres a lot of different ways but if you only want to DISPLAY the text and not STORE it anywhere then you just use: findstr /v "randomtextthatnoonewilluse" filename.txt

0

Corrected code :

setlocal enabledelayedexpansion
for /f "usebackq eol= tokens=* delims= " %%a in (`findstr /n ^^^^ "name with spaces.txt"`) do (
    set line=%%a
    set "line=!line:*:=!"
    echo(!line!
)
endlocal
pause
Celes
  • 21
  • 5
  • Of the main code of the page. Quotes are not positionned correctly, the code is not suited for files with name with spaces. Mine is. I give here the trick of the ^^^^ – Celes Jun 16 '19 at 18:08
  • You're right with the quotes around the filename, but your code has some issues: `eol=` and `delims=` are superfluous, `set line=%%a` should use quotes and without toggling the delayed expansion mode, the code can't read exclamation marks – jeb Jun 17 '19 at 11:33
-6

A code that displays the contents of the myfile.txt file on the screen

set %filecontent%=0
type %filename% >> %filecontent%
echo %filecontent%