在Windows 7中,批处理文件可以删除超过10天的文件夹。

时间:2022-06-22 02:22:57

I want to create a batch file which should delete all subfolders of a folder which are older than 10 days, using Windows 7

我想创建一个批处理文件,它应该删除一个超过10天的文件夹的所有子文件夹,使用Windows 7。

Any help would be appreciated.

如有任何帮助,我们将不胜感激。

3 个解决方案

#1


41  

Adapted from this answer to a very similar question:

从这个答案到一个非常相似的问题:

FORFILES /S /D -10 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"

You should run this command from within your d:\study folder. It will delete all subfolders which are older than 10 days.

您应该从您的d:\学习文件夹中运行这个命令。它将删除超过10天的所有子文件夹。

The /S /Q after the rd makes it delete folders even if they are not empty, without prompting.

即使在没有提示的情况下,即使它们不是空的,也可以删除文件夹。

I suggest you put the above command into a .bat file, and save it as d:\study\cleanup.bat.

我建议您将上述命令放入一个.bat文件中,并将其保存为d:\study\cleanup.bat。

#2


7  

FORFILES /S /D -10 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"

FORFILES /S /D -10 /C "cmd /C IF @isdir == TRUE rd /S /Q @path"

I could not get Blorgbeard's suggestion to work, but I was able to get it to work with RMDIR instead of RD:

我不能让Blorgbeard的建议去工作,但是我可以让它使用RMDIR而不是RD:

FORFILES /p N:\test /S /D -10 /C "cmd /c IF @isdir == TRUE RMDIR /S /Q @path"

FORFILES /p N:\test /S /D -10 /C "cmd /C IF @isdir == TRUE RMDIR /S /Q @path"

Since RMDIR won't delete folders that aren't empty so I also ended up using this code to delete the files that were over 10 days and then the folders that were over 10 days old.

因为RMDIR不会删除不空的文件夹,所以我也用这段代码删除了超过10天的文件,然后删除了10天以上的文件夹。

FOR /d %%K in ("n:\test*") DO (

FOR /d %%K in ("n:\test*")

FOR /d %%J in ("%%K*") DO (

FOR /d %%J in(“%%K*”)

FORFILES /P %%J /S /M . /D -10 /C "cmd /c del @file"

FORFILES /P %%J /S /M。/D -10 /C "cmd /C del @file"

)

)

)

)

FORFILES /p N:\test /S /D -10 /C "cmd /c IF @isdir == TRUE RMDIR /S /Q @path"

FORFILES /p N:\test /S /D -10 /C "cmd /C IF @isdir == TRUE RMDIR /S /Q @path"

I used this code to purge out the sub folders in the folders within test (example n:\test\abc\123 would get purged when empty, but n:\test\abc would not get purged

我使用这个代码来清除测试中的文件夹中的子文件夹(示例n:\test\abc\123在空的时候会被清除,但是n:\test\abc不会被清除。

#3


0  

If you want using it with parameter (ie. delete all subdirs under the given directory), then put this two lines into a *.bat or *.cmd file:

如果您想使用它与参数(ie)。删除给定目录下的所有子目录),然后将这两条线放入*中。蝙蝠或*。cmd文件:

@echo off
for /f "delims=" %%d in ('dir %1 /s /b /ad ^| sort /r') do rd "%%d" 2>nul && echo rmdir %%d

and add script-path to your PATH environment variable. In this case you can call your batch file from any location (I suppose UNC path should work, too).

并将脚本路径添加到PATH环境变量中。在这种情况下,您可以从任何位置调用您的批处理文件(我认为UNC路径也应该工作)。

Eg.:

如:

YourBatchFileName c:\temp

(you may use quotation marks if needed)

(如有需要,可使用引号)

will remove all empty subdirs under c:\temp folder

在c:\temp文件夹下删除所有空的子目录?

YourBatchFileName

will remove all empty subdirs under the current directory.

将删除当前目录下的所有空子目录。

#1


41  

Adapted from this answer to a very similar question:

从这个答案到一个非常相似的问题:

FORFILES /S /D -10 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"

You should run this command from within your d:\study folder. It will delete all subfolders which are older than 10 days.

您应该从您的d:\学习文件夹中运行这个命令。它将删除超过10天的所有子文件夹。

The /S /Q after the rd makes it delete folders even if they are not empty, without prompting.

即使在没有提示的情况下,即使它们不是空的,也可以删除文件夹。

I suggest you put the above command into a .bat file, and save it as d:\study\cleanup.bat.

我建议您将上述命令放入一个.bat文件中,并将其保存为d:\study\cleanup.bat。

#2


7  

FORFILES /S /D -10 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"

FORFILES /S /D -10 /C "cmd /C IF @isdir == TRUE rd /S /Q @path"

I could not get Blorgbeard's suggestion to work, but I was able to get it to work with RMDIR instead of RD:

我不能让Blorgbeard的建议去工作,但是我可以让它使用RMDIR而不是RD:

FORFILES /p N:\test /S /D -10 /C "cmd /c IF @isdir == TRUE RMDIR /S /Q @path"

FORFILES /p N:\test /S /D -10 /C "cmd /C IF @isdir == TRUE RMDIR /S /Q @path"

Since RMDIR won't delete folders that aren't empty so I also ended up using this code to delete the files that were over 10 days and then the folders that were over 10 days old.

因为RMDIR不会删除不空的文件夹,所以我也用这段代码删除了超过10天的文件,然后删除了10天以上的文件夹。

FOR /d %%K in ("n:\test*") DO (

FOR /d %%K in ("n:\test*")

FOR /d %%J in ("%%K*") DO (

FOR /d %%J in(“%%K*”)

FORFILES /P %%J /S /M . /D -10 /C "cmd /c del @file"

FORFILES /P %%J /S /M。/D -10 /C "cmd /C del @file"

)

)

)

)

FORFILES /p N:\test /S /D -10 /C "cmd /c IF @isdir == TRUE RMDIR /S /Q @path"

FORFILES /p N:\test /S /D -10 /C "cmd /C IF @isdir == TRUE RMDIR /S /Q @path"

I used this code to purge out the sub folders in the folders within test (example n:\test\abc\123 would get purged when empty, but n:\test\abc would not get purged

我使用这个代码来清除测试中的文件夹中的子文件夹(示例n:\test\abc\123在空的时候会被清除,但是n:\test\abc不会被清除。

#3


0  

If you want using it with parameter (ie. delete all subdirs under the given directory), then put this two lines into a *.bat or *.cmd file:

如果您想使用它与参数(ie)。删除给定目录下的所有子目录),然后将这两条线放入*中。蝙蝠或*。cmd文件:

@echo off
for /f "delims=" %%d in ('dir %1 /s /b /ad ^| sort /r') do rd "%%d" 2>nul && echo rmdir %%d

and add script-path to your PATH environment variable. In this case you can call your batch file from any location (I suppose UNC path should work, too).

并将脚本路径添加到PATH环境变量中。在这种情况下,您可以从任何位置调用您的批处理文件(我认为UNC路径也应该工作)。

Eg.:

如:

YourBatchFileName c:\temp

(you may use quotation marks if needed)

(如有需要,可使用引号)

will remove all empty subdirs under c:\temp folder

在c:\temp文件夹下删除所有空的子目录?

YourBatchFileName

will remove all empty subdirs under the current directory.

将删除当前目录下的所有空子目录。