Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>Detection of CR/LF versus single LF seems also a little bit complicated.<br> Also NUL characters are problematic, as a FOR-Loop immediatly cancels the reading.</p> <p><em>Direct</em> binary reading seems therefore nearly impossible.</p> <p>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.</p> <pre><code>@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 ) </code></pre> <p><strong>Toggling between enable and disabled delayed expansion is neccessary</strong> for the safe working with strings, like <code>!</code> or <code>^^^xy!z</code>.<br> That's because the line <code>set "var=%%a"</code> is only safe with <em>DisabledDelayedExpansion</em>, else exclamation marks are removed and the carets are used as (secondary) escape characters and they are removed too.<br> But using the variable <code>var</code> is only safe with <em>EnabledDelayedExpansion</em>, as even a <code>call %%var%%</code> will fail with content like <code>"&amp;"&amp;</code>.</p> <p><strong>EDIT: Added set/p variant</strong><br> There is a second way of reading a file with <code>set /p</code>, the only disadvantages are that it is limited to ~1024 characters per line and it removes control characters at the line end.<br> But the advantage is, you didn't need the delayed toggling and it's easier to store values in variables</p> <pre><code>@echo off setlocal EnableDelayedExpansion set "file=%~1" for /f "delims=" %%n in ('find /c /v "" %file%') do set "len=%%n" set "len=!len:*: =!" &lt;%file% ( for /l %%l in (1 1 !len!) do ( set "line=" set /p "line=" echo(!line! ) ) </code></pre> <p><strong>For reading it "binary" into a hex-representation</strong><br> You could look at <a href="https://stackoverflow.com/a/4648636/463115">SO: converting a binary file to HEX representation using batch file</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload