[英]How to delete all files in a directory and subdirectories except for certain folders?
Each month, I print a number of reports. 每个月,我都会打印一些报告。 After printing, I ftp them to a website. 打印后,我将它们通过ftp下载到一个网站。 To avoid ftping the same files each time, I want to delete all the files in the directory. 为了避免每次通过ftp传输相同的文件,我想删除目录中的所有文件。 Not all the folders need to have files deleted, however. 但是,并非所有文件夹都需要删除文件。 Is it possible to delete all the files in a directory and subdirectories excluding 3 or 4 folders? 是否可以删除目录和子目录中3个或4个文件夹以外的所有文件?
I know you can do something like: 我知道您可以执行以下操作:
del /s /q "C:\Users\Owner\OneDrive\test\*.*"
to delete all the files. 删除所有文件。 I am very unfamiliar with cmd, and I know it's possible to use if statements, but I'm not sure how they are syntaxed. 我对cmd非常不熟悉,我知道可以使用if语句,但是我不确定它们的语法如何。 Ideally, I would have something that works like: 理想情况下,我将具有类似以下的功能:
if(folderName != "Batchsheets" || folderName != "Statements" || folderName != "Histories")
{
folder.delete();
}
Any help would be appreciated, thanks! 任何帮助,将不胜感激,谢谢!
Edit: The folder structure looks like this: The main directory I'll be deleting from looks like this: 编辑:文件夹结构如下所示:我将从中删除的主目录如下所示:
C:\\Users\\Owner\\SharePoint\\EDSIReports - Reports\\Reports C:\\ Users \\ Owner \\ SharePoint \\ EDSIReports-报告\\报告
Inside that folder 在那个文件夹里
I have something like 50 folders. 我有大约50个文件夹。 All of the folders I want to delete are just numbers (examples: 1007, 1008, 1087, 12100, 20100). 我要删除的所有文件夹都是数字(例如:1007、1008、1087、12100、20100)。 The ones I want to leave have names like Batchsheets, Histories, Statements, BlankStatements, Invoices, and InterestPaid). 我要离开的人的名字有“批处理表”,“历史记录”,“对帐单”,“空白帐单”,“发票”和“ interestPaid”。 So I want to delete all of the pdfs stored in the folders with digit names like 1008 without deleting the files in Batchsheets. 因此,我想删除数字名称(如1008)存储在文件夹中的所有pdf,而不删除Batchsheets中的文件。
One easy way of doing it is to use robocopy
. 一种简单的方法是使用robocopy
。 The idea is to create an empty folder and sync this empty folder over your folder structure. 这个想法是创建一个空文件夹,并在您的文件夹结构上同步该空文件夹。 This will remove anything (there is nothing in the source, so, syncthing it makes the target empty). 这将删除任何内容(源中没有任何内容,因此,同步它会使目标为空)。 Now the trick is to exclude the required directories from the operation. 现在的诀窍是从操作中排除所需的目录。
cd /d "C:\Users\Owner\SharePoint\EDSIReports - Reports\Reports"
md _empty_ & robocopy _empty_ . /nocopy /purge /xd Batchsheets Statements Histories BlankStatements Invoices InterestPaid /L
If you prefer to remove the all digit folders, then you can use 如果您希望删除所有数字文件夹,则可以使用
for /d %a in (*) do @(for /f "delims=0123456789" %b in ("%a") do @break) || echo rmdir /s /q "%a"
For each folder, if removing all digits (we will use them as delimiters) leave us with an empty string the inner for /f
will fail (it has nothing to process) and the conditional operator ||
对于每个文件夹,如果删除所有数字(我们将它们用作定界符),并给我们留下一个空字符串for /f
的内部将失败(无法处理),并且条件运算符||
(execute next command if the previous failed) will execute the rmdir
(如果前一个失败,则执行下一个命令)将执行rmdir
Or, we can enumerate the all digit folders and remove them 或者,我们可以枚举所有数字文件夹并将其删除
for /f "delims=" %a in('dir /ad /b ^| findstr /x "[0-9]*"') do @echo rmdir /s /q "%a"
note: 注意:
all commands have been written to be used in the command line. 所有命令均已编写为可在命令行中使用。 To use inside a batch file, the for
replaceable arguments ( %a
and %b
) need to be escaped, doubling the percent signs ( %%a
, %%b
) 要在批处理文件中使用,需要转义for
可替换参数( %a
和%b
),使百分号( %%a
, %%b
)加倍
The rmdir
commands have beed preceded by a echo
to avoid problems while debugging. rmdir
命令之前带有echo
以避免调试时出现问题。 If the output is correct, remove the echo
如果输出正确,请删除echo
The robocopy
command includes a /L
swith. robocopy
命令包含一个/L
开关。 It will make the robocopy
command list what will be processed. 它将使robocopy
命令列出将要处理的内容。 If the output is correct, remove the /L
switch. 如果输出正确,请卸下/L
开关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.