简体   繁体   English

批处理文件以递归方式删除超过N天的文件夹中的文件

[英]Batch file to recursively delete files in a folder older than N number of days

I'm using a batch file now to delete all files ending in .snp that are older than 180 days. 我现在使用批处理文件删除以.snp结尾的所有超过180天的文件。 The code below works to delete all files ending in .snp under the root folder 下面的代码用于删除根文件夹下以.snp结尾的所有文件

C:\\Program Files\\Snapshots C:\\ Program Files \\ Snapshots

But I recently discovered that within the Snapshots folder there are folders organized by date 但我最近发现在Snapshots文件夹中有按日期组织的文件夹

"1-10-2014, 12-20-2014, 10-15-2014 etc.." “1-10-2014,12-20-2014,10-15-2014等......”

and that the below line of code doesn't work to recursively search through each directory and is therefore not deleting. 并且下面的代码行无法递归搜索每个目录,因此不会删除。

What changes should I make to this code to have it recursively search through folders within a root folder and delete files that are greater than 180 days? 我应该对此代码进行哪些更改,以便递归搜索根文件夹中的文件夹并删除超过180天的文件?

forfiles /M *.snp /P "C:\Program Files\Snapshots" /S /D -180 /C "cmd /c del /F /Q @path"

Without the /D (Date) it workes for sub-folders 没有/ D(日期)它适用于子文件夹

forfiles /M *.txt /P "C:\hlpme" /S /C "cmd /c del /f /q @path

but you obviously want the date to be there then in CMD 但你显然希望日期在CMD中

forfiles /D -180 /M *.txt /P "C:\hlpme" /S /C "cmd /c del /f /q @path

The /D before the Pathname selects all files that have been changed more than 180 days ago Pathname之前的/ D选择超过180天前已更改的所有文件

The best option for highest reliability is to combine the strengths of the For command with the FORFILES commands to allow each one to do what they do best. 最高可靠性的最佳选择是将For命令的强度与FORFILES命令相结合,以允许每个人执行他们最擅长的操作。

Set str_Ext=*.snp
Set int_Age=-180
For /R "%~dp0" %%D IN (.) DO (
    For /F "usebackq tokens=*" %%F IN (`FORFILES /P "%%~D" /m %str_Ext% /D %int_Age% 2^>nul`) DO (
        Call :s_Del_File "%%~D" "%%~F"
    )
)
Goto :EOF
:s_Del_File
Set "str_DIR=%~1"
Set "str_FIL=%~2"
Set "str_DIR=%str_DIR:~0,-1%"
DEL /F/Q/A "%str_DIR%%str_FIL%"
Goto :EOF

Within the second FOR command, the backquote (~ key) contains the FORFILES command and uses the console output to call a batch subroutine to delete the specified file. 在第二个FOR命令中,反引号(〜键)包含FORFILES命令,并使用控制台输出调用批处理子例程来删除指定的文件。

Spaces in folder and file names will not slow this beast down, and the double quotes ["] around the Set commands will allow the process to work with folders and files that have parentheses in them or other exotic, but allowable characters. 文件夹和文件名中的空格不会减慢此动作速度,并且Set命令周围的双引号[“]将允许进程处理其中包含括号或其他奇特但允许的字符的文件夹和文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理文件删除超过N天的文件 - Batch file to delete files older than N days 批处理文件可删除N天之前的文件并将结果另存为文本文件 - Batch file to delete files older than N days and save the result as a text file 如果所有文件都超过3天,则批量文件删除目录中的子文件夹 - Batch file to delete subfolders in directory if all files is older than 3 days Windows 批处理文件删除超过 X 天的文本文件 - Windows Batch file to delete text files older than X days 在Windows 7上批处理作业以删除7天以上的文件夹 - Batch job to delete folder older than 7 days on windows 7 Windows 批处理文件删除早于 X 天的文件,但保留一分钟的 X 文件 - Windows batch file delete files older than X days but leave a min of X files 批量删除早于x天的FTP上的文件 - Batch delete files on FTP older than x days 如何删除以子字符串结尾且早于 N 天的文件 - How to delete files end with a substring and older than N days Windows批处理脚本,用于基于文件名搜索文件,检查文件是否早于N天,如果为true,则删除这些文件 - Windows batch script for searching files based on file name,checks if the files are older than N days, if true, it deletes those files 如何使用forfiles(或类似)删除超过n天的文件,但总是留下最近的n - How to use forfiles (or similar) to delete files older than n days, but always leaving most recent n
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM