DOS 배치 파일에 IF 블록을 사용할 수 있습니까?
DOS 배치 파일에서 if 문 본문은 한 줄만 가질 수 있습니까? C와 같은 프로그래밍 언어에서 사용되는 ()것과 같은 if 블록에 사용할 수있는 어딘가를 찾았다 고 생각 {}하지만 이것을 시도 할 때 문을 실행하지 않습니다. 오류 메시지도 없습니다. 이 내 코드 :
if %GPMANAGER_FOUND%==true(echo GP Manager is up
goto Continue7
)
echo GP Manager is down
:Continue7
이상하게도 배치 파일을 실행할 때 "GP Manager is up"도 "GP Manager is down"도 인쇄되지 않습니다.
실제로 조건문 다음에 실행할 문 블록을 만들 수 있습니다. 그러나 구문이 잘못되었습니다. 괄호는 표시된대로 정확하게 사용해야합니다.
if <statement> (
do something
) else (
do something else
)
그러나 else-if명령문에 대한 기본 제공 구문이 있다고 생각하지 않습니다 . 불행히도 if이를 처리 하기 위해 중첩 된 문 블록을 만들어야 합니다.
둘째, 그 %GPMANAGER_FOUND% == true테스트는 나에게 매우 의심스러워 보입니다. 환경 변수가 무엇으로 설정되어 있는지 또는 어떻게 설정하고 있는지는 모르지만 표시 한 코드가 원하는 결과를 생성 할 것이라고 확신하지 못합니다.
다음 샘플 코드가 잘 작동합니다.
@echo off
if ERRORLEVEL == 0 (
echo GP Manager is up
goto Continue7
)
echo GP Manager is down
:Continue7
내 샘플 코드에 대한 몇 가지 구체적인 세부 정보를 참고하십시오.
- 조건 문의 끝과 여는 괄호 사이에 추가 된 공백입니다.
- 내가 설정하고
@echo off보는 것을 유지하기 위해 모든 그들이 실행으로 콘솔에 인쇄 된 진술을하고, 대신 특별히로 시작하는 것들의 출력을 참조echo. - 기본 제공
ERRORLEVEL변수를 테스트로 사용하고 있습니다. 여기에서 더 많은 것을 읽으 십시오
Logically, Cody's answer should work. However I don't think the command prompt handles a code block logically. For the life of me I can't get that to work properly with any more than a single command within the block. In my case, extensive testing revealed that all of the commands within the block are being cached, and executed simultaneously at the end of the block. This of course doesn't yield the expected results. Here is an oversimplified example:
if %ERRORLEVEL%==0 (
set var1=blue
set var2=cheese
set var3=%var1%_%var2%
)
This should provide var3 with the following value:
blue_cheese
but instead yields:
_
because all 3 commands are cached and executed simultaneously upon exiting the code block.
I was able to overcome this problem by re-writing the if block to only execute one command - goto - and adding a few labels. Its clunky, and I don't much like it, but at least it works.
if %ERRORLEVEL%==0 goto :error0
goto :endif
:error0
set var1=blue
set var2=cheese
set var3=%var1%_%var2%
:endif
Instead of this goto mess, try using the ampersand & or double ampersand && (conditional to errorlevel 0) as command separators.
I fixed a script snippet with this trick, to summarize, I have three batch files, one which calls the other two after having found which letters the external backup drives have been assigned. I leave the first file on the primary external drive so the calls to its backup routine worked fine, but the calls to the second one required an active drive change. The code below shows how I fixed it:
for %%b in (d e f g h i j k l m n o p q r s t u v w x y z) DO (
if exist "%%b:\Backup.cmd" %%b: & CALL "%%b:\Backup.cmd"
)
I ran across this article in the results returned by a search related to the IF command in a batch file, and I couldn't resist the opportunity to correct the misconception that IF blocks are limited to single commands. Following is a portion of a production Windows NT command script that runs daily on the machine on which I am composing this reply.
if "%COPYTOOL%" equ "R" (
WWLOGGER.exe "%APPDATA%\WizardWrx\%~n0.LOG" "Using RoboCopy to make a backup of %USERPROFILE%\My Documents\Outlook Files\*"
%TOOLPATH% %SRCEPATH% %DESTPATH% /copyall %RCLOGSTR% /m /np /r:0 /tee
C:\BIN\ExitCodeMapper.exe C:\BIN\ExitCodeMapper.INI[Robocopy] %TEMP%\%~n0.TMP %ERRORLEVEL%
) else (
WWLOGGER.exe "%APPDATA%\WizardWrx\%~n0.LOG" "Using XCopy to make a backup of %USERPROFILE%\My Documents\Outlook Files\*"
call %TOOLPATH% "%USERPROFILE%\My Documents\Outlook Files\*" "%USERPROFILE%\My Documents\Outlook Files\_backups" /f /m /v /y
C:\BIN\ExitCodeMapper.exe C:\BIN\ExitCodeMapper.INI[Xcopy] %TEMP%\%~n0.TMP %ERRORLEVEL%
)
Perhaps blocks of two or more lines applies exclusively to Windows NT command scripts (.CMD files), because a search of the production scripts directory of an application that is restricted to old school batch (.BAT) files, revealed only one-command blocks. Since the application has gone into extended maintenance (meaning that I am not actively involved in supporting it), I can't say whether that is because I didn't need more than one line, or that I couldn't make them work.
Regardless, if the latter is true, there is a simple workaround; move the multiple lines into either a separate batch file or a batch file subroutine. I know that the latter works in both kinds of scripts.
Maybe a bit late, but hope it hellps:
@echo off
if %ERRORLEVEL% == 0 (
msg * 1st line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue1
)
:Continue1
If exist "C:\Python31" (
msg * 2nd line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue2
)
:Continue2
If exist "C:\Python31\Lib\site-packages\PyQt4" (
msg * 3th line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue3
)
:Continue3
msg * 4th line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue4
)
:Continue4
msg * "Tutto a posto" rem You can relpace msg * with any othe operation...
pause
참고URL : https://stackoverflow.com/questions/4983508/can-i-have-an-if-block-in-dos-batch-file
'Programing' 카테고리의 다른 글
| jquery가있는 jsonp (0) | 2020.09.08 |
|---|---|
| 이 동사 유형으로 콘텐츠 본문을 보낼 수 없습니다. (0) | 2020.09.08 |
| 명령 줄 응용 프로그램에서 키보드로 입력 (0) | 2020.09.07 |
| 여러 줄 문자열 리터럴의 구문은 무엇입니까? (0) | 2020.09.07 |
| Eclipse에서 Tomcat 8.5.x 및 TomEE 7.x를 사용하는 방법은 무엇입니까? (0) | 2020.09.07 |