Batch files: how to read a file?

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

+89
file batch-file
Oct. 15 '08 at 19:37
source share
9 answers

You can use the for command:

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

Type of

 for /? 

on the command line. In addition, you can parse ini files !

+40
Oct 15 '08 at 19:47
source share

In the style of cmd.exe in the style of NT, you can scroll through the lines of a text file with

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

Type "help for" at the command line for more information. (I don’t know if this works in all the "DOS" that you use)

+64
Oct. 15 '08 at 19:45
source share

Normally FOR-LOOP works, but there are some problems. FOR does not accept empty lines and lines with more than ~ 8190 are problematic. An extension only works reliably if a pending extension is disabled.

Detecting CR / LF compared to single LF also seems a bit complicated.
NUL characters are also problematic because FOR-Loop immediately cancels readings.

Direct binary reading seems almost impossible.

The problem with empty lines can be solved with a trick. Prefix each line with 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 ) 

Switching between enabled and disabled extension delays is required for safe work with strings, for example ! or ^^^xy!z .
This is because the string set "var=%%a" is only safe with DisabledDelayedExpansion, the rest of the exclamation marks are deleted, and carriages are used as (secondary) escape characters, and they are also deleted.
But using the var variable is only safe with EnabledDelayedExpansion, since even call %%var%% will fail with content like "&"& .

EDIT: Added option set / p
There is a second way to read a file with set /p , the only drawback is that it is limited to ~ 1024 characters per line and removes the control characters at the end of the line.
But the advantage is that you did not need delayed switching, and it is 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 "binary" in hexadecimal representation
You can look at SO: converting a binary to a HEX representation using a batch file

+35
Dec 25 '10 at 17:54
source share

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

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

This displays only the first line of text in the text file. Another way you can do this is to use the following command:

 type %pathtotextfile%\textfile.txt 

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

+6
Aug 10 '17 at 23:28
source share

Well, you have many different ways, but if you only want to display the text and not store it anywhere, then you simply use: findstr /v "randomtextthatnoonewilluse" filename.txt

+1
Sep 13 '16 at 21:32
source share

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 
0
Jun 14 '19 at 14:29
source share

Using:
set/p var=< file.ext
This is the easiest way to do this, it works on most versions of the package.

-one
May 18 '18 at 16:21
source share

Code that displays the contents of the myfile.txt file on the screen

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

-2
Oct 18 '18 at 15:29
source share

if you want to just display it on cmd you can use this:

 cat myfile.txt 
-four
Dec 22 '14 at 2:43 on
source share



All Articles