[英]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.