Celeb Glow
updates | March 06, 2026

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.

1

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

Calling the function:

call:myDosFunc

For usage on IF EXISTS, see this.

You can redirect stderr to null:

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

ref

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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