批量脚本删除一个子文件夹并保留另一个子文件夹,同时删除其中的所有文件?

时间:2021-12-18 02:07:24

I have something that I could use help with. Let me give you the folder layout as an example:

我有一些可以帮助的东西。我举个文件夹布局作为例子:

C:...\Logs\SubfolderA

C:...\Logs\SubfolderB

Each folder contains multiple logs as text files. What I would like to be able to do is to have a batch script which deletes the folder SubfolderB completely, but clears out the files in that folder SubfolderA except for ONE file, while preserving SubfolderA.

每个文件夹包含多个日志作为文本文件。我想要做的是有一个批处理脚本完全删除文件夹SubfolderB,但清除该文件夹SubfolderA中除一个文件以外的文件,同时保留SubfolderA。

Alternately, I'll need another one that deletes the folder SubfolderB completely, then deletes everything in SubfolderA while keeping the folder. Once I have an idea of how to do the one that I first mentioned, however, the next one should be easy enough for me to do.

或者,我需要另一个完全删除文件夹SubfolderB,然后在保留文件夹的同时删除SubfolderA中的所有内容。一旦我知道如何做我刚才提到的那个,那么下一个对我来说应该很容易。

Currently, I am just running a script from each folder that either deletes all the files in its perspective folder except for the batch file, or deletes all files except for the batch file AND the one file that I want to preserve (the SubfolderA batch script does this), however it would be a lot easier to kill two birds with one stone and only have to execcute one file, if that is possible.

目前,我只是从每个文件夹运行一个脚本,删除其透视文件夹中除批处理文件以外的所有文件,或者删除除批处理文件和我要保留的一个文件之外的所有文件(SubfolderA批处理脚本)这样做,但是如果可能的话,用一块石头杀死两只鸟并且只需要执行一个文件会容易得多。

Thanks!

1 个解决方案

#1


0  

You can delete a directory (including its contents) with rd /s /q foo. You can delete all files in a directory using del /f /q foo\*.

您可以使用rd / s / q foo删除目录(包括其内容)。您可以使用del / f / q foo \ *删除目录中的所有文件。

To delete all but one file in a directory you need some criterion that applies to the one file you are keeping, and then it requires a loop over that directory's files:

要删除目录中除一个文件之外的所有文件,您需要一些适用于您保留的文件的标准,然后它需要在该目录的文件上循环:

for %%F in (foo\*) do if not "%%F"=="bar" del "%%F"

#1


0  

You can delete a directory (including its contents) with rd /s /q foo. You can delete all files in a directory using del /f /q foo\*.

您可以使用rd / s / q foo删除目录(包括其内容)。您可以使用del / f / q foo \ *删除目录中的所有文件。

To delete all but one file in a directory you need some criterion that applies to the one file you are keeping, and then it requires a loop over that directory's files:

要删除目录中除一个文件之外的所有文件,您需要一些适用于您保留的文件的标准,然后它需要在该目录的文件上循环:

for %%F in (foo\*) do if not "%%F"=="bar" del "%%F"