Multiline text file, how to put into an environment variable

后端 未结 2 580
野的像风
野的像风 2020-12-19 11:44

i have a file called file.txt which contains:

     this is line one ^
     this is line two ^
     this is the last line

how can i put that

2条回答
  •  独厮守ぢ
    2020-12-19 11:49

    As dbenham said, it can be done also with for/f but it's a bit more complicated.

    The simple 80% solution is

    setlocal EnableDelayedExpansion
    
    set "var="
    set LF=^
    
    
    rem *** Two empty lines are required for the linefeed
    FOR /F "delims=" %%a in (myFile.txt) do (
      set "var=!var!!LF!%%a"
    )
    echo !var!
    

    But it fails with:
    - If a line is blank it will be skipped
    - If a line begins with ; the EOL-character
    - If a line contains ! (and carets)

    But then you could use a bit more complex solution

    @echo off
    SETLOCAL DisableDelayedExpansion
    set "all="
    FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ aux1.txt"`) do (
        set "line=%%a"
        SETLOCAL EnableDelayedExpansion
        set "line=!line:#=#S!"
        set "line=!line:*:=!"
        for /F "delims=" %%p in ("!all!#L!line!") do (
            ENDLOCAL
            set "all=%%p"
        )
    )
    SETLOCAL EnableDelayedExpansion
    if defined all (
    set "all=!all:~2!"
    set ^"all=!all:#L=^
    
    !"
    set "all=!all:#S=#!"
    )
    echo !all!
    

    What the code do?
    First, the findstr /n ^^ will prepend each line with a line number and a colon, like

    1:My first Line
    2:; beginning with a semicolon
    3:
    4:there was an empty line
    

    This solves the problem of empty lines and also the standard EOL-character ; can be ignored.
    To get the content of the line, the value is set to a variable while delayed expansion is disabled, this solves the problem with ! and ^ characters.

    To remove the line number and the colon, the delayed expansion will be enabled (no, a delim of : can't solve it).

    Then all # are replaced with #S, this will be done first, as after the prefix removing the line could be empty and the replacement would fail.
    But why I replace it?
    That's because I can't insert the linefeeds here, as the following FOR/F would fail with embedded linefeeds,
    so I only add linefeed marker (in this case I use #L), but the content of the file could contain also a #L, but by replacing all # with #S all markers are unique.

    After the marker, there is the problem to close/disable the delayed expansion with an endlocal, but preserve the content of the modified all and line variable.
    This is done with the FOR/F-endlocal trick, as the %%p can transport content behind the endlocal barrier.

    Then after reading the complete file, I check if the all is defined, as it would be empty for an empty file.
    Then the first linefeed marker #L will be removed, and all other markers are replaced with a real linefeed character.
    Then the sharp safer #S will be reverted to #.

    That's all, so even this solution is obviously...

提交回复
热议问题