使用命令行删除目录及其文件,但如果目录不存在则不要抛出错误 [英] Delete a directory and its files using command line but don't throw error if it doesn't exist
问题描述
我需要Windows命令来删除目录及其所有包含的文件,但是如果目录不存在,我不想看到任何错误.
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.
推荐答案
将del
命令的输出重定向到nul.注意2
,以指示应重定向错误输出.另请参见此问题,尤其是技术文档使用命令重定向运算符.
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>nul
或者您可以在调用del
之前检查文件是否存在:
Or you can check for file existence before calling del
:
if exist c:\folder\file del c:\folder\file
请注意,您可以使用if exist c:\folder\
(后跟\
)检查c:\folder
是否确实是文件夹而不是文件.
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.
这篇关于使用命令行删除目录及其文件,但如果目录不存在则不要抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!