Windows: How to specify multiline command on command prompt?

后端 未结 4 734
Happy的楠姐
Happy的楠姐 2020-11-28 22:47

how do we extend a command to next line?

basically whats the windows alternative for linux\'s

ls -l \\
/usr/

here we use backslash

4条回答
  •  眼角桃花
    2020-11-28 23:32

    If you came here looking for an answer to this question but not exactly the way the OP meant, ie how do you get multi-line CMD to work in a single line, I have a sort of dangerous answer for you.

    Trying to use this with things that actually use piping, like say findstr is quite problematic. The same goes for dealing with elses. But if you just want a multi-line conditional command to execute directly from CMD and not via a batch file, this should do work well.

    Let's say you have something like this in a batch that you want to run directly in command prompt:

    @echo off
    for /r %%T IN (*.*) DO (
        if /i "%%~xT"==".sln" (
            echo "%%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file
            echo Dumping SLN file contents
            type "%%~T"
        )
    )
    

    Now, you could use the line-continuation carat (^) and manually type it out like this, but warning, it's tedious and if you mess up you can learn the joy of typing it all out again.

    Well, it won't work with just ^ thanks to escaping mechanisms inside of parentheses shrug At least not as-written. You actually would need to double up the carats like so:

    @echo off ^
    More? for /r %T IN (*.sln) DO (^^
    More? if /i "%~xT"==".sln" (^^
    More? echo "%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file^^
    More? echo Dumping SLN file contents^^
    More? type "%~T"))
    

    Instead, you can be a dirty sneaky scripter from the wrong side of the tracks that don't need no carats by swapping them out for a single pipe (|) per continuation of a loop/expression:

    @echo off
    for /r %T IN (*.sln) DO if /i "%~xT"==".sln" echo "%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file | echo Dumping SLN file contents | type "%~T"
    

提交回复
热议问题