.bat 无意中删除子目录

.bat deleting subdirectories unintentionally

pushd "\svr01\folderA\"     
forfiles /P . /D -2 /C "cmd /C del /Q @path && cmd /c echo [%DATE%%TIME%] Deleted file @path" >> %LOGFILE%    
popd

为什么要删除我的子目录的内容? :F 我认为它不会,除非我在命令中包含一个“/S”

似乎 "IF @ISDIR==FALSE echo" 是解决方法?

forfiles /P . /D -2 /C "cmd /C IF @ISDIR==FALSE del /Q @path && cmd /c echo [%DATE%%TIME%] Deleted file @path" >> %LOGFILE%  

它不会删除目录的全部内容,但会删除目录中的所有文件

引用 DEL 帮助:

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

names: Specifies a list of one or more files or directories. Wildcards may be used to delete multiple files. If a directory is specified, all files within the directory will be deleted.

编辑: 是的,@isdir 检查可以避免这个问题。

DEL Delete one or more files.

If a folder name is given instead of a file, all files in the folder will be deleted, but the folder itself will not be removed.

使用if /I "@isdir"=="FALSE"检查如下:

forfiles /P . /D -2 /C "cmd /C if /I "@isdir"=="FALSE" (del /Q @path && echo [%DATE% %TIME%] Deleted file @fdate @path")