Do Batch files support multiline variables

前端 未结 9 1811
南旧
南旧 2020-12-25 12:54

If so How?

Yes, batch files are lame, but I cannot use powershell, and I don\'t feel like writing a real app to do this simple task....

edit

相关标签:
9条回答
  • 2020-12-25 13:09

    I think I figured it out. I had to add ^ to the %NL% var when putting it in the string, otherwise it hides the text after the first %NL% because its executing when being set, instead of when its being echoed later

    set NL=^& echo
    
    set str=text on line 1 ^%NL% text on line 2
    
    echo %str%
    text on line 1
    text on line 2
    
    0 讨论(0)
  • 2020-12-25 13:10

    This is a pretty old question, but I put together a hybrid of the solutions from @Fabricio and @jeb that both worked correctly and added some readability:

    setlocal enableDelayedExpansion
    set NL=^
    
    
    rem two empty line required
    set var=this is a !NL! ^
    multi !NL! ^
    line !NL! ^
    string !NL!
    
    echo !var!
    
    0 讨论(0)
  • 2020-12-25 13:13
    SET myFlags= ^
        a ^
        b ^
        c
    
    0 讨论(0)
  • 2020-12-25 13:17

    Or you can create a "real" newline character.

    setlocal enableDelayedExpansion
    set NL=^
    
    
    rem two empty line required
    echo first line !NL! second line
    set multi=Line1!NL!Line2
    set multi=!multi!!NL!Line3
    echo !Multi!
    

    With this variant the newline is a "normal" character in the string, so the variables act normally and you can assign them to another variable, this is not possible with the &echo. trick (which is useful for simple tasks).

    0 讨论(0)
  • 2020-12-25 13:21

    If you just wanted to use it for outputting the lines of variables, I hope this would be useful to you.


    Inspired by something, I found this working.

    For codes in normal Command Prompt:

    set this=echo hi ^& echo bye

    %this%

    It gives out the two following two lines:

    hi
    bye
    

    in the Command Prompt window.


    The reason for using the ^ character:

    Command Prompt would identify that you wanted to run two commands so that you need to add this character, that tells Command Prompt to recognize the next character as a symbol and not to do any special thing for it.

    0 讨论(0)
  • 2020-12-25 13:22

    And now - without auxiliary SET commands:

    echo this is a & echo multi & echo line & echo string
    
    0 讨论(0)
提交回复
热议问题