Do Batch files support multiline variables

前端 未结 9 1813
南旧
南旧 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:25

    I prefer this method

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    
    REM (define NewLine character)
    (SET _NL=^
    %=this line is empty=%
    )
    
    0 讨论(0)
  • 2020-12-25 13:26

    If you wanted to build something instead, not just outputting, hope this would be useful to you too.


    Firstly, you should make a file. Such as .txt. I'm going to use var.txt as an example.

    Secondly, type in the multi-lines of variables.

    Thirdly, here are the codes, but only works for batch:

    @echo off
    set num=0
    set /p this%num%=<"var.txt"
    goto there
    
    :there
    set /a num=num+1
    for /f "skip=%num%" %%a in (var.txt) do (set this%num% =%%a
    goto there)
    set num=0
    goto build
    
    :build
    rem Example of using multi-lines of variable.
    call echo %%this%num% %%
    set /a num=num+1
    for /f "skip=%num% delims=" %%a in (var.txt) do (call echo %%this%num% %%
    goto build)
    rem Just replace the commands with `echo` part into something else which suits your situation.
    pause >nul
    exit
    

    I admit that strictly speaking, this is not a variable of multi-line, but in my opinion, this is better than nothing.

    You may ask why not just use the setlocal EnableDelayedExpansion command. The reason is simply, that command does work for me (IDK why). Therefore, I thought and made these codes that make the same effect.

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

    Is that ok?

    @echo off
    set var=kur
    set var2=kur2
    echo var is = "%var%" and var2 is = %var2%
    

    edit
    is that what you mean ?

    @echo off
    set nl=^& echo.
    echo this%nl%is%nl%multiline%nl%string
    
    0 讨论(0)
提交回复
热议问题