Delete file if found
I have a batch script with a line like this:
del "%TARGET_DIR%\packages.config"But if packages.config does not exist in the target directory it produces an error.
Could Not Find
\\...\packages.config
Is there a command line switch del that will prevent raising an error the target file is not found? I'm open to using an if exists if I have to, but I have a number of similar commands, and I'd like to avoid having to do that for every single file I might want to delete.
2 Answers
You could functionalize your IF EXISTS/DEL calls so that the code isn't repeated a ton; for doing faux functions in DOS, see this.
They use GOTO statements to implement functions, but it should work the same.
From that page:
Example Function:
:myDosFunc - here starts my function identified by it`s label
echo. here the myDosFunc function is executing a group of commands
echo. it could do a lot of things
GOTO:EOFCalling the function:
call:myDosFuncFor usage on IF EXISTS, see this.
You can redirect stderr to null:
del "%TARGET_DIR%\packages.config" 2> nul 1