Tjs
Tjs

Reputation: 423

return value from batch script

I am calling batch script from vbscript.

In the batch script, based on condition I want to return custom exit code.

@echo off
setlocal ENABLEDELAYEDEXPANSION`
for /f "usebackq" %%i in (`"%1"\fciv.exe" -md5 %2"`) do set md5_1=%%i  
for /f "usebackq" %%i in (`"%1"\fciv.exe" -md5 %3"`) do set md5_2=%%i

if "!md5_1!" == "!md5_2!" (  
    set md5_1=  
    set md5_2=  
    exit 0  
) else (  
    set md5_1=  
    set md5_2=  
    exit 1  
)
endlocal  

I am getting value 0 for both of the conditions.

can anybody help me?

Upvotes: 3

Views: 10931

Answers (2)

Sebastian
Sebastian

Reputation: 6493

The batch processor (cmd) will actually retain the errorlevel of the last call. To use this feature, do not call endlocal at the end of the script, this is called implicitly anywayMSDN Besides, always use exit /b inside scripts, as the normal exit call will also exit the current batch processor instance

Upvotes: 4

foxidrive
foxidrive

Reputation: 41287

Both your commands look to have mismatched quotes and can fail with long names. Try this

You aren't used delayed expansion and nulling the variables is pointless when a setlocal has been issued.

Usebackq is not needed.

Finally it's bad practice to use %%i because it looks so much like %%l and %%1 Microsoft uses it in examples, I know.

@echo off
setlocal
for /f %%a in ('"%~1\fciv.exe" -md5 "%~2"') do set md5_1=%%a  
for /f %%a in ('"%~1\fciv.exe" -md5 "%~3"') do set md5_2=%%a

if "%md5_1%"=="%md5_2%" (exit /b 0) else (exit /b 1 )
endlocal

Upvotes: 0

Related Questions