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
jeb Dec 25 '10 at 17:54 2010-12-25 17:54
source share