Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file Can';t从批处理文件中的子例程返回结果_Batch File - Fatal编程技术网

Batch file Can';t从批处理文件中的子例程返回结果

Batch file Can';t从批处理文件中的子例程返回结果,batch-file,Batch File,以下是我尝试使用的批处理文件: @ECHO off setlocal FOR %%R IN ("SRC" "COMMON" "SCRIPTS") DO ( call :CheckRepo %%R IF ERRORLEVEL 0 (@echo Repository %%R revision is top) else (@echo Repository %%R is revision wrong) ) goto END :CheckRepo pushd . cd %1 FOR /F "d

以下是我尝试使用的批处理文件:

@ECHO off
setlocal
FOR %%R IN ("SRC" "COMMON" "SCRIPTS") DO (
  call :CheckRepo %%R
  IF ERRORLEVEL 0 (@echo Repository %%R revision is top) else (@echo Repository %%R is revision wrong)
)

goto END

:CheckRepo
pushd .
cd %1
FOR /F "delims=^+^ " %%A IN ('hg id') DO (set revision=%%A)

FOR /F "tokens=1,3 delims=: " %%A IN ('hg branches') DO (
  IF "%%A" EQU "default" (
    IF "%%B" EQU "%revision%" (goto EXIT_OK) else (goto EXIT_ERROR)
  )
)

:EXIT_OK
popd
@echo This repository is on top revision
Exit /B 0

:EXIT_ERROR
popd
@echo This repository is NOT on top revision. Top is %revision%
Exit /B 1

:END
ERRORLEVEL始终为零,即使我在某些目录的输出中有“此存储库不在顶部修订版”。但在顶层,它要么为所有目录打印“Repository is revision Error”,要么为所有目录再次打印“Repository revision is top”。就像它将某个值写入ERRORLEVEL,然后使用它并忽略通过退出返回的值一样

尝试执行:

@ECHO off
setlocal
FOR %%R IN ("SRC" "COMMON" "SCRIPTS") DO (
  call :CheckRepo %%R && (
    @echo Repository %%R revision is top ) || (
    @echo Repository %%R is revision wrong
  )
)
尝试执行:

@ECHO off
setlocal
FOR %%R IN ("SRC" "COMMON" "SCRIPTS") DO (
  call :CheckRepo %%R && (
    @echo Repository %%R revision is top ) || (
    @echo Repository %%R is revision wrong
  )
)

您可以使用此errorlevel检查来测试特定值

如果“%errorlevel%”==“0”

或者如果需要


如果“!errorlevel!”==“0”

您可以使用此errorlevel检查来测试特定值

如果“%errorlevel%”==“0”

或者如果需要


如果“!errorlevel!”==“0”

是,条件执行有效,但为什么它不能与errorlevel一起工作?因为
如果errorlevel 0
告诉您,如果errorlevel为零或更高。@user2846246-请参阅Stephan的评论。是,条件执行有效,但是为什么它不能与ERRORLEVEL一起工作呢?因为
如果ERRORLEVEL 0
告诉您,如果ERRORLEVEL为零或更高。@user2846246-请参阅Stephan的评论。在这种情况下,如果没有同意延迟扩展,这将无法工作。我刚刚指出,可以专门测试%errorlevel%,而不是errorlevel提供的等于或大于该测试的测试。在这种情况下,如果不同意延迟扩展,这将不起作用。我刚刚指出,可以专门测试%errorlevel%,而不是errorlevel提供的相等或更大的测试。