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.
Windows – Delete a directory and its files using command line but don’t throw error if it doesn’t exist
batch-filecmdwindows
Related Topic
- Windows – “rm -rf” equivalent for Windows
- Windows batch files: .bat vs .cmd
- Windows – How to get current date/time on the Windows command line in a suitable format for usage in a file/folder name
- Windows – n equivalent of ‘which’ on the Windows command line?
- Windows – How to get the application exit code from a Windows command line
- Windows – How to delete files/subfolders in a specific directory at the command prompt in Windows
- What does %~dp0 mean, and how does it work
- How to “comment-out” (add comment) in a batch/cmd
Best Answer
Redirect the output of the
del
command to nul. Note the2
, to indicate error output should be redirected. See also this question, and especially the tech doc Using command redirection operators.Or you can check for file existence before calling
del
:Note that you can use
if exist c:\folder\
(with the trailing\
) to check ifc:\folder
is indeed a folder and not a file.