I don't think that my question is duplicate to batch: Exit code for "rd" is 0 on error as well because I've tried everything from that answer and it didn't help (as you can see on screenshots below).
Let's say I want to delete directory with files in it. But I don't want script to continue executing in case of an error. So I tried to:
ECHO 3. Deleting Client index folder which will be rebuilt if it actually exist...
rmdir /S /Q "XXX"
ECHO %ERRORLEVEL%
IF %ERRORLEVEL% NEQ 0 ECHO Failed to delete client index folder... & GOTO :End
But %ERRORLEVEL% variable was always equal 0.
Then I found similar questions on Stackoverflow and changed script to this:
ECHO 3. Deleting Client index folder which will be rebuilt if it actually exist...
rmdir /S /Q "XXX" && echo OK || echo ERROR
ECHO %ERRORLEVEL%
IF %ERRORLEVEL% NEQ 0 ECHO Failed to delete client index folder... & GOTO :End
But the problem is that I want script to continue working if folder doesn't exist (i.e. was already deleted). With that implementation it wasn't working.
So I changed script like this:
ECHO 3. Deleting Client index folder which will be rebuilt if it actually exist...
if exist "XXX" (rmdir /S /Q "%4_%5" && echo OK || echo ERROR)
ECHO %ERRORLEVEL%
IF %ERRORLEVEL% NEQ 0 ECHO Failed to delete client index folder... & GOTO :End
Aaaaaaaand it stopped setting up %ErrorLevel% variable again in case of file lock...
I've tried to remove brackets after "exist" condition. Tried to change "if exist" to "2>nul", but none of these options work as I need. Any suggestions on how I should fix it?