Script package does not raise ERRORLEVEL on failure

I am studying windows command script. I need to raise errors if copy, move, or delete operations fail. I made a sample script and for some reason, when the operation fails, I cannot get the ERRORLEVEL to raise. I run the script and the files either don't exist or open in another program and stderr messages are printed to the console, but ERRORLEVEL never gets raised, why is that? Also, is there a way to pass stderr into a variable that I could check if I can't get the ERRORLEVEL to raise?

My code looks like this:

`@Echo Off
ECHO.
Set /P myVar=Please enter a value:
Echo var = %myVar%
ECHO Trying to delete the file dummy.csv >> log.txt
Set myVar2 = nothing
DEL C:\dummy420.csv  
IF NOT ERRORLEVEL 0 ECHO There was an error
ECHO %ERRORLEVEL%
REM 2>myVar2
REM Echo 2>
REM Echo %myVar2%
Echo 2>&1
REM && (echo yourCommand was successful) || (echo yourCommand failed)
IF NOT ERRORLEVEL 0 ECHO There was an error
ECHO %ERRORLEVEL%
Move ""C:\dummy420.csv" D:\"
IF NOT ERRORLEVEL 0 ECHO There was an error
Set /P dummy=Press Enter to End`

      

This is my conclusion:

Please enter a value:hello
var = hello
C:\dummy420.csv
The process cannot access the file because it is being used by another process.
0
ECHO is off.
0
The filename, directory name, or volume label syntax is incorrect.
Press Enter to End

      

+3


source to share


1 answer


DEL

command does not change error level - details Check debenham suggestion for unsuccessful removal:

3>&2 2>&1 1>&3 del C:\dummy420.csv|findstr . && echo DEL failed || echo DEL succeeded

      



to change the error level:

3>&2 2>&1 1>&3 del C:\dummy420.csv|findstr .&& cmd /c exit /b 1

      

+1


source







All Articles