0

I am facing a problem with my following batch script, where I can see how the execution of the command %nr% --f !path2! never seems to happen and I don't understand the reason.

What am I doing wrong? Too many nested conditions ?

EDIT: adding WRONG code where comments are enabled

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
                    )

                ) 

            ) 

        )

    )
)

Thanks!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
ndarkness
  • 1,011
  • 2
  • 16
  • 36
  • Most likely `%PATH1%` or `%PATH2%` does not exist. You may want to insert `echo "!PATH1!" && dir !PATH1!` before `if exist !PATH1!` (and the same for `PATH2`) just to make sure. Defensive programming. Or maybe `%NR%` sets `%ERRORLEVEL%` to non-zero. Check with `echo ERRORLEVEL=!ERRORLEVEL!`. – AlexP Jun 25 '18 at 12:30
  • @AlexP thanks for th reply, both paths exist I have double checked it and the inputs as well( they are files) – ndarkness Jun 25 '18 at 12:32
  • @AlexP `%ERRORLEVEL%` is zero as well, I have checked it – ndarkness Jun 25 '18 at 12:35
  • 2
    Why are you assigning the command line arguments to an environmental variable and then assigning that environmental variable to another environmental variable. You are over coding that. You could do this: set "path1=!path!%~1". Regardless of that, you are using the system `PATH` variable which can have dozens of paths assigned to it. The `IF EXIST !path1!` command will never be TRUE because of that. You can only use `IF` exist on a single directory or file path. – Squashman Jun 25 '18 at 12:46
  • 2
    It would certainly help were we to see the content of `%1`, `%2`, `%3` and `!path!`. Asking us to assess code where the most important elements are a mystery is a little over optimistic. – Compo Jun 25 '18 at 12:49
  • Have you *actually checked* in the batch file as I suggested? Can you edit the question and post the results? – AlexP Jun 25 '18 at 12:49
  • 3
    Are you sure that you want to create a variable named `%myPath"%`, because the rest of your code is trying to use one names `%myPath%`. Perhaps you should replace `set "myPath"=%3"` with `set "myPath=%3"`! – Compo Jun 25 '18 at 13:05

2 Answers2

1

I suggest following batch code:

@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

An error condition is detected as soon as possible with resulting in exiting batch file processing with an appropriate error message and exit code.

There is no need for delayed environment variable expansion which makes processing the batch file faster and avoids problems with directory or file names containing an exclamation mark.

Name of a file or the file path can contain also command line critical characters like space or one of these characters &()[]{}^=;!'+,`~.

There are no nested IF conditions making successful execution flow straight from top to bottom.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~1 which expands to argument 1 with removing any surrounding quotes.
  • echo /?
  • endlocal /? ... used here implicit on exiting batch file processing with exit and explicit at end of batch file.
  • exit /?
  • goto /?
  • if /?
  • rem /?
  • setlocal /?

See also Single line with multiple commands using Windows batch file.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

I think I found the issue, it has to do with the fact that the "echos" to debug seem to influence the script execution. So in the code of my question, if I enable the echos, the script will fail running whereas if I disable them( commented out as below), the script does work.

Nevertheless, I don't understand why it does fail with the echos in first instance.

Code with disabled echos and working

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
                    )

                ) 

            ) 

        )

    )
)
ndarkness
  • 1,011
  • 2
  • 16
  • 36
  • And you are saying it had nothing to do with the fact that you were using the system path variable earlier. Also pretty much all of your code will fail whenever there is a space or special character in the file path because you are not putting quotes around the variables. – Squashman Jun 25 '18 at 14:43
  • @Squashman, yes I am saying it didn't have to do with the path variable earlier. – ndarkness Jun 25 '18 at 14:56
  • Really. Then why did you change your code? The `IF EXIST` would have never been TRUE because you assigned the system path variable to the variable you were checking with your `IF EXIST`. – Squashman Jun 25 '18 at 15:04
  • @Squashman it was a typo, I never intended to use the system path rather than the path provided as input. To be frank, I even didn't know that the `path` variable is the one for the path in windows cmd – ndarkness Jun 26 '18 at 06:12
  • I think you have been here long enough to post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Squashman Jun 26 '18 at 13:11