So basically I'm trying to make a batch file, that executes a command and upon successfully executing that command exit. But if it fails to execute the first command, it should run a second (backup) command and then exit.
For example, I want a .bat file, that upon launching, deletes a file in some directory, and then exit. But if it gets an error in trying to do so, it should try to delete another file in different directory and then exit.
Lets just say, I created a batch file that deletes "a.txt" from the current directory and exits. But if it fails, it should then try to delete "b.txt" and exits. But it should delete b.txt ONLY IF IT FAILS TO DELETE a.txt.
I wanna utilize IF, ELSE and %ERRORLEVEL% in doing so.
Here's a code that I'm trying to do it with right now:
@echo off
del a.txt
IF %ERRORLEVEL% NEQ 0 (
del b.txt
) else (
ECHO victory
)
In the above code I'm using the errorlevel code 0, which executes the plan b if its not equal to 0 (which is returned on a successful execution of the command). Right now it does not delete the b.txt.
Please help me in creating a stable and working one...
Thanks in advance
The detection of result of del command is not so easy The dbenham's solution form the link above:
3>&2 2>&1 1>&3 del "a.txt"|findstr . && (
echo DEL failed
del "b.txt"
color
)||(
echo DEL succeeded
)