3

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.

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

2 Answers 2

3

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:EOF

Calling the function:

call:myDosFunc

For usage on IF EXISTS, see this.

2

You can redirect stderr to null:

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

ref

1
  • 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, 2014 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .