I want to delete files from a folder and if there are no files to delete, error should be ignored and all the other errors should be captured. Below is the code I am using.
FOR /F "tokens=*" %%a IN ('FORFILES /P "%%G" /D -3 /C "cmd /c DEL @path" 2^>^&1 ^| FINDSTR ERROR') DO SET _CmdResult=%%a
IF "%_CmdResult%" == "ERROR: No files found with the specified search criteria." (
SET errorlevel=0
) ELSE (
SET errorlevel=1
)
IF "%_CmdResult%" == "NONE" SET errorlevel=0
IF NOT %errorlevel% EQU 0 (
goto delete_fail )
Below is what I see on command prompt when I run above code.
FOR /F "tokens=*" %a IN ('FORFILES /P "%G" /D -2 /C "cmd /c DEL @path" | FINDSTR ERROR') DO SET _CmdResult=%a
IF "NONE" == "ERROR: No files found with the specified search criteria." <<<<< _CmdResult is still "NONE"
(
SET errorlevel=0
) ELSE (
SET errorlevel=1 )
IF "NONE" == "NONE" SET errorlevel=0
IF NOT 0 EQU 0 (goto delete_fail )
Based on the log I found that error string is not getting saved into _CmdResult when there are not files to delete. Can anyone let me know what I am missing,
The %%G indicates this line is executed as part of an outer for loop. If that loop includes the rest of the code shown, then you have a problem with delayed expansion. When execution reaches a block (code between parenthesis), all lines are parsed before execution, and the lines in which variables are readed are changed, replacing the reference to the variable with the value it contains at the time of parsing of the block. You will need to enable delayed expansion and instead of
%var%
sintax, use the!var!
sintax.In short, don't do that. Don't set the errorlevel variable to any value. Its value is dynamic, provided by cmd when readed, but, if a value is explicitly assigned, the dynamic value can not be read. %errorlevel% will return the value you set to it. If you set it to 0, you will only get this value, independly of sucess of failure of commands. It will be 0.
If you need to reset the errorlevel value, use something like
vol > nul