为什么我的if语句块似乎没有批量到达?

时间:2018-06-25 12:23:35

标签: batch-file

我的后续批处理脚本遇到了问题,在这里我可以看到命令%nr% --f !path2!似乎从未执行过,我也不理解原因。

我在做什么错?嵌套条件太多了吗?

编辑:添加启用了注释的 WRONG 代码

rem The call to this batch script will be this
rem C:/Projects/DevelopmentTools/SDKs/TP/B/Scrpt/Exg_Serial_Flasher.bat EF.hex DH.hex C:/Projects/DevelopmentTools/SDKs/TP/B/E/Output/CN/Exe/

setlocal enabledelayedexpansion

set nr=nr.exe

if "%1"=="" (
    if "%2"=="" (
        if "%3"=="" (
            echo "[Error]"
            set "runScript="
            )
    )
) else (

    set "input1=%1"
    set "input2=%2"
    set "path3=%3%nr%"
    set "myPath"=%3"
    set "path1=!myPath!!input1!"
    set "path2=!myPath!!input2!"

    rem Control variable 
    set "runScript=true"
)

if defined runScript (

    if exist "%path3%" (

        %nr% --check

        if exist !path1! (

            %nr% --f !input1!

            echo !ERRORLEVEL!
            if !ERRORLEVEL! EQU 0 (

                echo !input1! set correctly
                if exist !path2! (

                    echo Setting !exgSerial!
                    %nr% --f !input2! 

                    if !ERRORLEVEL! EQU 0 (
                        echo Everything went fine
                    )

                ) 

            ) 

        )

    )
)

谢谢!

2 个答案:

答案 0 :(得分:1)

我建议使用以下批处理代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ExeFile=nr.exe"

if "%~1" == "" goto ArgumentError
if "%~2" == "" goto ArgumentError
if not "%~3" == "" goto ProcessArguments

:ArgumentError
echo Error: %~nx0 must be called with three arguments.
exit /B 1

:ProcessArguments
rem Assign third argument to an environment variable.
set "FilePath=%~3"
rem Replace forward slashes by backslashes which is the directory separator on Windows.
set "FilePath=%FilePath:/=\%"
rem Make sure the file path ends with a backslash.
if not "%FilePath:~-1%" == "\" set "FilePath=%FilePath%\"

set "HexFile1=%FilePath%%~1"
set "HexFile2=%FilePath%%~2"
set "ExeFile=%FilePath%%ExeFile%"

if not exist "%ExeFile%"  echo Error: "%ExeFile%" does not exist. & exit /B 2
if not exist "%HexFile1%" echo Error: "%HexFile1%" does not exist. & exit /B 3
if not exist "%HexFile2%" echo Error: "%HexFile2%" does not exist. & exit /B 3

"%ExeFile%" --check

"%ExeFile%" --f "%HexFile1%"
if errorlevel 1 echo Error: Processing "%HexFile1%" failed. & exit /B 4

"%ExeFile%" --f "%HexFile2%"
if errorlevel 1 echo Error: Processing "%HexFile2%" failed. & exit /B 4

echo Everything worked fine.
endlocal

尽快检测到错误情况,导致退出批处理文件并显示相应的错误消息和退出代码。

不需要延迟环境变量扩展,这可以加快批处理文件的处理速度,并避免包含感叹号的目录或文件名出现问题。

文件名或文件路径也可以包含命令行关键字符,例如空格或这些字符之一&()[]{}^=;!'+,`~

没有嵌套的 IF 条件,可以使成功的执行从上到下直接进行。

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • call /? ...解释%~1并扩展为参数1并删除所有引号。
  • echo /?
  • endlocal /? ...此处隐式表示使用exit退出批处理文件处理,并在批处理文件末尾显式使用。
  • exit /?
  • goto /?
  • if /?
  • rem /?
  • setlocal /?

另请参阅Single line with multiple commands using Windows batch file

答案 1 :(得分:0)

我认为我发现了问题,这与以下事实有关:要调试的“ echos”似乎会影响脚本的执行。因此,在我的问题代码中,如果启用回声,脚本将无法运行,而如果禁用回声(如下所述),则脚本可以正常工作。

尽管如此,我不明白为什么它在第一次出现回声时会失败。

禁用回声并正常工作的代码

rem The call to this batch script will be this
rem C:/Projects/DevelopmentTools/SDKs/TP/B/Scrpt/Exg_Serial_Flasher.bat EF.hex DH.hex C:/Projects/DevelopmentTools/SDKs/TP/B/E/Output/CN/Exe/

setlocal enabledelayedexpansion

set nr=nr.exe

if "%1"=="" (
    if "%2"=="" (
        if "%3"=="" (
            echo "[Error]"
            set "runScript="
            )
    )
) else (

    set "input1=%1"
    set "input2=%2"
    set "path3=%3%nr%"
    set "myPath"=%3"
    set "path1=!myPath!!input1!"
    set "path2=!myPath!!input2!"

    rem Control variable 
    set "runScript=true"
)

if defined runScript (

    if exist "%path3%" (

        %nr% --check

        if exist !path1! (

            %nr% --f !input1!

            rem echo !ERRORLEVEL!
            if !ERRORLEVEL! EQU 0 (

                rem echo !input1! set correctly
                if exist !path2! (

                    rem echo Setting !exgSerial!
                    %nr% --f !input2! 

                    if !ERRORLEVEL! EQU 0 (
                        echo Everything went fine
                    )

                ) 

            ) 

        )

    )
)