Cmd file put content of file in var with newline

Viewed 38

i'm working on a cmd script. In it i read a file and place the result in a variable My file look like something like this:

First line (Blank line) Second line (Blank line) And it goes on like this

In my code i'm using this:

For /f "Tokens=* Delims=" %%x in (temp.txt) do set content=!content!%%x

As a result everithing is on one line i don't have my blank line : First line second line ... How can i get the blank line ?

1 Answers

Your for loop doesn't fetch the empty line, because empty lines are skipped.
You need a trick, like prepending each line with a line number, with findstr.
And to add line feeds you can defined a variable containing a single line feed character (in the code the \n variable)

@echo off
SETLOCAL EnableDelayedExpansion
(set \n=^
%=do not delete this line=%
)

set "content="
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t7.bat"`) do (
    set "var=%%a"
    set "var=!var:*:=!"
    if defined content (
        set "content=!content!!\n!!var!"
    ) else set "content=!var!"
)

echo(!content!

This code has still some problems, with exclamation marks, but for simple file content it works

Related