0

I have a batch file with 10 lines and 5 functions in a batch script. How can I ensure that all the commands in a batch file are successful.

In other way, what's the logic to calculate return code of each command at the end of a script.

1. @ECHO OFF

 2. if not exist "%Destination%\%NAME%" md %Destination%\%NAME%

 3. if not exist "%Destination%\%NAME2%" md %Destination%\%NAME2%

 4. rmdir %Destination%\%NAME3%
 5. if not exist "%Destination%\NAME4%" md %Destination%\%NAME4%
 6. cd /d X:\test1

in the above 5 lines, 4th line returns %ERRORLEVEL% 1 and 6th line returns the same. But, I could not put IF %ERRORLEVEL%==0 after every command. So, how could i script to handle this.

Ras
  • 543
  • 1
  • 9
  • 25
  • show an example – sensorario Apr 27 '18 at 21:28
  • 2
    Instead of checking the existence use `md %Destination%\%NAME% 2>Nul` to suppress errors. Otherwise use [conditional execution](https://ss64.com/nt/syntax-conditional.html) with `||` on fail and `&&` on success. –  Apr 27 '18 at 21:54
  • In Line 4 the path is not double quoted, in case of spaces in `%Destination%` this might fail. –  Apr 27 '18 at 21:58
  • I want those lines to be failed. But, I want return code of each line and function at the end of a script. – Ras Apr 27 '18 at 22:10
  • 1
    If you _want_ the error level to be 1 for lines 4 and 6, why not use `if %errorlevel%==1` for those two lines and `if %errorlevel%==0` for the other lines? – SomethingDark Apr 28 '18 at 02:17

2 Answers2

0

You should firstly save your file as .cmd instead of .bat for better error handling. Also always enclose your paths with double quotes. Then I suggest you test existance as well to overcome errorlevel.

If exist "%Destination%\%NAME3%" rmdir "%Destination%\%NAME3%"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

For the code example I suggest following:

@echo off
rem Verify the existence of all used environment variables.
for %%I in (Destination NAME NAME2 NAME3 NAME4) do (
    if not defined %%I (
        echo Error detected by %~f0:
        echo/
        echo Environment variable name %%I is not defined.
        echo/
        exit /B 4
    )
)

rem Verify the existence of all used directories by creating them
rem independent on existing already or not and next verifying if
rem the directory really exists finally.
for %%I in ("%Destination%\%NAME%" "%Destination%\%NAME2%") do (
    md %%I 2>nul
    if not exist "%%~I\" (
        echo Error detected by %~f0:
        echo/
        echo Directory %%I
        echo does not exist and could not be created.
        echo/
        exit /B 3
     )
)

rem Remove directories independent on their existence and verify
rem if the directories really do not exist anymore finally.
for %%I in ("%Destination%\%NAME3%") do (
    rd /Q /S %%I 2>nul
    if exist "%%~I\" (
        echo Error detected by %~f0:
        echo/
        echo Directory %%I
        echo still exists and could not be removed.
        echo/
        exit /B 2
     )
)

cd /D X:\test1 2>nul
if /I not "%CD%" == "X:\test1" (
    echo Error detected by %~f0:
    echo/
    echo Failed to set "X:\test1" as current directory.
    echo/
    exit /B 1
)

This batch file handles nearly all possible errors which could occur during execution of this batch file. A remaining problem could be caused by an environment variable containing one or more double quotes in its value. The solution would be using delayed expansion.

Linux shell script interpreters have the option -e to exit immediately execution of a script if any command or application returns with a value not equal 0. But Windows command interpreter cmd.exe does not have such an option. The options of cmd.exe can be read on running in a command prompt window cmd /?.

So it is necessary to use in a batch file:

  • if exist "..." exit /B 1 or goto :EOF
  • if not exist "..." exit /B 1 or goto :EOF
  • if errorlevel 1 exit /B 1 or goto :EOF
  • ... || exit /B 1 or ... || goto :EOF

See also the Stack Overflow articles:

Mofi
  • 46,139
  • 17
  • 80
  • 143