Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a directory and its files using command line but don't throw error if it doesn't exist

I need a Windows command to delete a directory and all its containing files but I do not want to see any errors if the directory does not exist.

like image 292
jaywayco Avatar asked Jan 24 '13 13:01

jaywayco


People also ask

How do I delete a file that doesn't exist?

Right click the bad file and click Add to Archives. Then in the options select to delete original file after compression. Leave other options same and proceed. Bad file will be deleted and a compressed file will be created in its place.

How do I delete a file that won't delete in CMD?

To do this, start by opening the Start menu (Windows key), typing run, and hitting Enter. In the dialogue that appears, type cmd and hit Enter again. With the command prompt open, enter del /f filename, where filename is the name of the file or files (you can specify multiple files using commas) you want to delete.

What command will delete a directory with files in it?

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can contain only .


1 Answers

Redirect the output of the del command to nul. Note the 2, to indicate error output should be redirected. See also this question, and especially the tech doc Using command redirection operators.

del {whateveroptions} 2>null 

Or you can check for file existence before calling del:

if exist c:\folder\file del c:\folder\file 

Note that you can use if exist c:\folder\ (with the trailing \) to check if c:\folder is indeed a folder and not a file.

like image 164
GolezTrol Avatar answered Sep 29 '22 02:09

GolezTrol