无法从批处理文件中的子例程返回结果

时间:2016-01-21 17:17:30

标签: 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 "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< name>是修正错误"对于所有目录,或者"存储库< name>修订版是顶级"再次为所有目录。就像它将一些值写入ERRORLEVEL然后只使用它并忽略通过Exit

返回的值

2 个答案:

答案 0 :(得分:0)

尝试执行conditional

@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
  )
)

答案 1 :(得分:0)

您可以使用此错误级别检查来测试特定值。

if "%errorlevel%" == "0"

或者如果需要......

if "!errorlevel!" == "0"