.BAT 파일 내에서 여러 .BAT 파일을 실행하는 방법
commit-build.bat
빌드 프로세스의 일부로 다른 .BAT 파일을 실행 하려고합니다 .
내용 commit-build.bat
:
"msbuild.bat"
"unit-tests.bat"
"deploy.bat"
이것은 충분히 간단 해 보이지만 commit-build.bat
목록 ( msbuild.bat
) 의 첫 번째 항목 만 실행합니다 .
문제없이 각 파일을 개별적으로 실행했습니다.
사용하다:
call msbuild.bat
call unit-tests.bat
call deploy.bat
CALL을 사용하지 않으면 현재 배치 파일이 중지되고 호출 된 배치 파일이 실행되기 시작합니다. MS-DOS 초기 시절로 거슬러 올라가는 독특한 행동입니다.
다른 모든 대답은 정확합니다. 통화를 사용하십시오. 예를 들면 :
call "msbuild.bat"
역사
고대 DOS 버전에서는 배치 파일을 재귀 적으로 실행할 수 없었습니다. 그런 다음 다른 cmd 셸을 호출하여 배치 파일을 실행하고 완료되면 호출하는 cmd 셸로 다시 실행을 반환하는 호출 명령이 도입되었습니다.
분명히 이후 버전에서는 더 이상 다른 cmd 셸이 필요하지 않았습니다.
초기에는 많은 배치 파일이 배치 파일 호출이 호출 배치 파일로 돌아 가지 않는다는 사실에 의존했습니다. 추가 구문없이 해당 동작을 변경하면 배치 메뉴 시스템 (메뉴 구조에 배치 파일 사용)과 같은 많은 시스템이 손상 될 수 있습니다.
Microsoft의 많은 경우와 마찬가지로 이전 버전과의 호환성이 이러한 동작의 원인입니다.
팁
배치 파일 이름에 공백이있는 경우 이름을 따옴표로 묶습니다.
call "unit tests.bat"
참고로 배치 파일의 이름이 모두없는 경우 for를 사용하여이 작업을 수행 할 수도 있습니다 (배치 파일 호출의 올바른 순서를 보장하지 않으며 파일 시스템의 순서를 따릅니다).
FOR %x IN (*.bat) DO call "%x"
호출 후 오류 수준에 대응할 수도 있습니다. 사용하다:
exit /B 1 # Or any other integer value in 0..255
오류 수준을 돌려줍니다. 0은 올바른 실행을 나타냅니다. 호출 배치 파일에서 다음을 사용하여 반응 할 수 있습니다.
if errorlevel neq 0 <batch command>
if errorlevel 1
NT4 / 2000 / XP보다 오래된 Windows를 사용 하여 모든 오류 수준 1 이상을 포착하는 경우 사용 합니다.
배치 파일의 흐름을 제어하려면 goto :-(
if errorlevel 2 goto label2
if errorlevel 1 goto label1
...
:label1
...
:label2
...
다른 사람들이 지적했듯이 배치 파일을 대체 할 빌드 시스템을 살펴보십시오.
여러 명령 프롬프트를 열려면 다음을 사용할 수 있습니다.
start cmd /k
/k
: 실행할 필수입니다.
다음과 같이 많은 명령 프롬프트를 실행할 수 있습니다.
start cmd /k Call rc_hub.bat 4444
start cmd /k Call rc_grid1.bat 5555
start cmd /k Call rc_grid1.bat 6666
start cmd /k Call rc_grid1.bat 5570.
시험:
call msbuild.bat
call unit-tests.bat
call deploy.bat
프로그램을 컴파일하기 위해 여러 배치를 호출하고 있습니다. 오류가 발생하면
1) 배치 내의 프로그램이 오류 수준으로 종료됩니다.
2) 당신은 그것에 대해 알고 싶습니다.
for %%b in ("msbuild.bat" "unit-tests.bat" "deploy.bat") do call %%b|| exit /b 1
'||' tests for an errorlevel higher than 0. This way all batches are called in order but will stop at any error, leaving the screen as it is for you to see any error message.
call msbuild.bat
call unit-tests.bat
call deploy.bat
To call a .bat
file within a .bat
file, use
call foo.bat
(Yes, this is silly, it would make more sense if you could call it with foo.bat
, like you could from the command prompt, but the correct way is to use call
.)
If we have two batch scripts, aaa.bat and bbb.bat, and call like below
call aaa.bat
call bbb.bat
When executing the script, it will call aaa.bat first, wait for the thread of aaa.bat terminate, and call bbb.bat.
But if you don't want to wait for aaa.bat to terminate to call bbb.bat, try to use the START command:
START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/AFFINITY <hex affinity>] [/WAIT] [/B] [command/program]
[parameters]
Exam:
start /b aaa.bat
start /b bbb.bat
Looking at your filenames, have you considered using a build tool like NAnt or Ant (the Java version). You'll get a lot more control than with bat files.
Start msbuild.bat
Start unit-tests.bat
Start deploy.bat
If that doesn't work, replace start
with call
or try this:
Start msbuild.bat
Goto :1
:1
Start unit-tests.bat
Goto :2
:2
Start deploy.bat
If you want to open many batch files at once you can use the call command. However, the call command closes the current bat file and goes to another. If you want to open many at once, you may want to try this:
@echo off
start cmd "call ex1.bat&ex2.bat&ex3.bat"
And so on or repeat start cmd
"call
..." for however many files. This works for Windows 7, but I am not sure about other systems.
Running multiple scripts in one I had the same issue. I kept having it die on the first one not realizing that it was exiting on the first script.
:: OneScriptToRunThemAll.bat
CALL ScriptA.bat
CALL ScriptB.bat
EXIT
:: ScriptA.bat
Do Foo
EXIT
::ScriptB.bat
Do bar
EXIT
I removed all 11 of my scripts EXIT lines and tried again and all 11 ran in order one at a time in the same command window.
:: OneScriptToRunThemAll.bat
CALL ScriptA.bat
CALL ScriptB.bat
EXIT
::ScriptA.bat
Do Foo
::ScriptB.bat
Do bar
Just use the call
command! Here is an example:
call msbuild.bat
call unit-tests.bat
call deploy.bat
With correct quoting (this can be tricky sometimes):
start "" /D "C:\Program Files\ProgramToLaunch" "cmd.exe" "/c call ""C:\Program Files\ProgramToLaunch\programname.bat"""
1st arg - Title (empty in this case)
2nd arg - /D specifies starting directory, can be ommited if want the current working dir (such as "%~dp0")
3rd arg - command to launch, "cmd.exe"
4th arg - arguments to command, with doubled up quotes for the arguments inside it (this is how you escape quotes within quotes in batch)
Here is My Way =
cmd /c ""Path_Bat_Here.bat" %ARG%"
:: Or =
Call *.bat
Help = cmd /?
The following solved my issue:
start call "batch1.bat"
start call "batch2.bat"
참고URL : https://stackoverflow.com/questions/1103994/how-to-run-multiple-bat-files-within-a-bat-file
'developer tip' 카테고리의 다른 글
@selector () in Swift? (0) | 2020.10.02 |
---|---|
CSS 배경 늘리기 및 크기 조정 (0) | 2020.10.02 |
정규식 : AND 연산자가 있습니까? (0) | 2020.10.02 |
객체 리터럴 / 이니셜 라이저의 자체 참조 (0) | 2020.10.02 |
버튼을 클릭 할 때 대화 상자가 닫히지 않도록하는 방법 (0) | 2020.09.30 |