基本上, 可以说我有一个示例文件夹,如下所示:
C:\
└───Dox
│ cat.txt
│ dog.txt
│ girl.txt
│ ...
│
└───NonDOx
boy.txt
girl.txt
...
我希望它看起来像这样
C:\
└───Dox
│ girl.txt
│
└───NonDOx
girl.txt
因此,是的,基本上,应该删除文件夹和子文件夹中的所有文件,但girl.txt
除外,后者位于随机文件夹中。
另外,文件夹和目录有什么区别?像一个
目录是一个包含一个或多个文件夹的文件夹,或者与文件夹相同?
我找不到要在子文件夹中删除并留下1(特定文件)的内容。
答案 0 :(得分:1)
我将通过以下方式进行操作:
rem /* Loop over all items (both files and sub-directories) in the root directory recursively;
rem then sort them in reverse alphabetic manner, so lower directory hierarchy levels appear
rem before higher ones and files appear before their parent sub-directories: */
for /F "delims= eol=|" %%F in ('dir /S /B /A "C:\Dox\*.*" ^| sort /R') do (
rem // Check whether current item is a file (note the trailing `\` to match directories):
if not exist "%%F\" (
rem // The current item is a file, hence check its name and delete if not matching:
if /I not "%%~nxF" == "girl.txt" del "%%F"
) else (
rem // The current item is a sub-directory, hence try to remove it; this only works
rem when it is empty; the `2> nul` prefix suppresses potential error messages:
2> nul rd "%%F"
)
)
答案 1 :(得分:0)
最简单(也是最安全)的删除所有...的方法是,仅将那些文件复制到备份文件夹,删除整个原始目录,然后将备份的文件放回原处。之所以称之为“安全”,是因为您可以在备份目录中检查是否存在所有需要的文件:(未测试)
xcopy /S girl.txt <Backup_Directory>\
cd <Backup_Directory>
tree /F //verify if everything is well copied
rmdir <original_directory>
xcopy /S <Backup_Directory>\ <original_directory>\