windowsbatch-filecmd

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.


Solution

  • 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.