Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF ERRORLEVEL 0 checking doesnt seem to work correctly

I have a minimal batch script on my windows machine desktop which uses the

FC.exe tool to compare two text files also on my desktop.

My files to compare

file1.txt contains one character a

file2.txt contains one character b

My batch file

SET "result=success"

fc.exe "%USERPROFILE%\Desktop\file1.txt" "%USERPROFILE%\Desktop\file2.txt"
IF NOT ERRORLEVEL 0 SET "result=failed"

echo %result%

pause

I expect the var result to be failed (as file1 is different to file2),

however the output shows it is success

I have tried changing the only character b to a in file2.txt and

running the batch file again, but the output shows the result var is still

success

Furthermore, I can append the following

...
echo %ERRORLEVEL% 
...

directly under the fc.exe cmd, and it prints 1 for the first test and 0 for

the second, this result should cause the IF loop to trigger but it does not.

What is the cause of this?

like image 560
DavidBattenburg Avatar asked Oct 28 '25 10:10

DavidBattenburg


1 Answers

IF ERRORLEVEL X checks if errorlevel is equal or greater than X so IF NOT ERRORLEVEL 0 checks if the errorlevel is less than 0. Use conditional execution or IF %ERRORLEVEL% NEQ 0

like image 74
npocmaka Avatar answered Oct 30 '25 01:10

npocmaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!