Program Tip

Windows 배치 파일을 통해 긴 명령을 여러 줄로 분할

programtip 2020. 9. 30. 11:23
반응형

Windows 배치 파일을 통해 긴 명령을 여러 줄로 분할


배치 파일에서 긴 명령을 여러 줄로 분할하려면 어떻게해야합니까?


당신은 캐럿으로 긴 줄을 분리 할 수 있습니다 ^만큼 당신이 캐럿하고 다음 줄 바꿈이 완전히 제거되었는지 기억한다. 따라서 줄을 끊는 곳에 공백이 있어야한다면 공백을 포함하십시오. ( 자세한 내용은 아래에 있습니다. )

예:

copy file1.txt file2.txt

다음과 같이 작성됩니다.

copy file1.txt^
 file2.txt

캐럿의 규칙은 다음과 같습니다.

줄 끝의 캐럿은 다음 줄을 추가하고 추가 된 줄의 첫 번째 문자는 이스케이프됩니다.

캐럿을 여러 번 사용할 수 있지만 전체 줄은 최대 줄 길이 인 ~ 8192 자 (Windows XP, Windows Vista 및 Windows 7)를 초과해서는 안됩니다.

echo Test1
echo one ^
two ^
three ^
four^
*
--- Output ---
Test1
one two three four*

echo Test2
echo one & echo two
--- Output ---
Test2
one
two

echo Test3
echo one & ^
echo two
--- Output ---
Test3
one
two

echo Test4
echo one ^
& echo two
--- Output ---
Test4
one & echo two

다음 문자의 이스케이프를 억제하려면 리디렉션을 사용할 수 있습니다.

The redirection has to be just before the caret. But there exist one curiosity with redirection before the caret.

If you place a token at the caret the token is removed.

echo Test5
echo one <nul ^
& echo two
--- Output ---
Test5
one
two


echo Test6
echo one <nul ThisTokenIsLost^
& echo two
--- Output ---
Test6
one
two

And it is also possible to embed line feeds into the string:

setlocal EnableDelayedExpansion
set text=This creates ^

a line feed
echo Test7: %text%
echo Test8: !text!
--- Output ---
Test7: This creates
Test8: This creates
a line feed

The empty line is important for the success. This works only with delayed expansion, else the rest of the line is ignored after the line feed.

It works, because the caret at the line end ignores the next line feed and escapes the next character, even if the next character is also a line feed (carriage returns are always ignored in this phase).


(This is basically a rewrite of Wayne's answer but with the confusion around the caret cleared up. So I've posted it as a CW. I'm not shy about editing answers, but completely rewriting them seems inappropriate.)

You can break up long lines with the caret (^), just remember that the caret and the newline that follows it are removed entirely from the command, so if you put it where a space would be required (such as between parameters), be sure to include the space as well (either before the ^, or at the beginning of the next line — that latter choice may help make it clearer it's a continuation).

Examples: (all tested on Windows XP and Windows 7)

xcopy file1.txt file2.txt

can be written as:

xcopy^
 file1.txt^
 file2.txt

or

xcopy ^
file1.txt ^
file2.txt

or even

xc^
opy ^
file1.txt ^
file2.txt

(That last works because there are no spaces betwen the xc and the ^, and no spaces at the beginning of the next line. So when you remove the ^ and the newline, you get...xcopy.)

For readability and sanity, it's probably best breaking only between parameters (be sure to include the space).

Be sure that the ^ is not the last thing in a batch file, as there appears to be a major issue with that.


Multiple commands can be put in parenthesis and spread over numerous lines; so something like echo hi && echo hello can be put like this:

( echo hi
  echo hello )

Also variables can help:

set AFILEPATH="C:\SOME\LONG\PATH\TO\A\FILE"
if exist %AFILEPATH% (
  start "" /b %AFILEPATH% -option C:\PATH\TO\SETTING...
) else (
...

Also I noticed with carets (^) that the if conditionals liked them to follow only if a space was present:

if exist ^

It seems however that splitting in the middle of the values of a for loop doesn't need a caret(and actually trying to use one will be considered a syntax error). For example,

for %n in (hello
bye) do echo %n

Note that no space is even needed after hello or before bye.

참고URL : https://stackoverflow.com/questions/69068/split-long-commands-in-multiple-lines-through-windows-batch-file

반응형