Delete the directory and its files using the command line, but do not throw an error if it does not exist.

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

+43
windows cmd batch-file
Jan 24 '13 at 13:34
source share
3 answers

Redirect the output of the del command to nul:

 del {whateveroptions} 2>nul 

Or you can check the existence of the file before calling del :

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

Note that you can use if exist c:\folder\nul or just if exist c:\folder\ (with the ending \ ) to check if c:\folder really a folder, not a file.

+51
Jan 24 '13 at
source share

Or redirect stderr to nul

 rd /q /s "c:\yourFolder" 2>nul 

Or make sure the folder exists before deleting. Note that the final \ is critical in the IF condition.

 if exist "c:\yourFolder\" rd /q /s "c:\yourFolder" 
+30
Jan 24 '13 at 15:14
source share

You can redirect stderr to nul

 del filethatdoesntexist.txt 2>nul 
+4
Jan 24 '13 at 13:45
source share



All Articles