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.

  • FYI, DEL /Q does not allow it to silently fail; it still complains. You're going to have to test ahead of time... sorry. – Lynn Crumbling Jan 14 '14 at 15:59

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

Calling the function:


For usage on IF EXISTS, see this.

| improve this answer | |

You can redirect stderr to null:

del "%TARGET_DIR%\packages.config" 2> nul


| improve this answer | |
  • Thanks, but I'm going with the other answer because I don't want to ignore all errors (e.g. if the file is in use), just if the file is not found. – p.s.w.g Jan 14 '14 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.