MS DOS批处理删除目录中的旧文件 [英] MS Dos Batch delete old files in directory
问题描述
可能重复:结果
批处理文件删除文件比N天年长
我试图做一个DOS批处理文件要经过一个目录中大约500,000文件吧,我想它删除所有旧的然后1年文件
I'm trying to make a DoS Batch file to go through a directory with about 500,000 files in it, and i would like it to delete all the files older then 1 year
下面是我的code到目前为止
Here's my code so far
@echo off
title File Exclusion Act
for /f "usebackq delims=|" %%f in (`dir /b "C:\Users\Travis\Desktop\LotsOfFiles"`) do echo %%f
pause
到目前为止,它循环并打印出指定目录下的所有文件。
So far it loops and prints out all the files in the specified directory.
任何提示/帮助是非常AP preciated。
Any tips/help is highly appreciated.
推荐答案
该批处理文件下面必须与旧文件的天数被要求从今天删除。例如,使用365删除1岁文件。
The Batch file below must be called with the number of days for old files to remove from today. For example, use 365 to remove 1 year old files.
@echo off
setlocal EnableDelayedExpansion
call :DateToJDN %date% oldDate= -%1
for /F "skip=5 tokens=1-4*" %%a in ('dir /A:-D /O:D') do (
call :DateToJDN %%a fileDate=
if !fileDate! lss %oldDate% (
del "%%e"
) else (
goto :EOF
)
)
goto :EOF
:DateToJDN Date JDN= [+-days]
for /F "tokens=1-3 delims=/" %%x in ("%1") do set /A mm=10%%x %% 100, dd=10%%y %% 100, yy=%%z
if %mm% lss 3 set /A mm+=12, yy-=1
set /A a=yy/100, b=a/4, c=2-a+b, e=36525*(yy+4716)/100, f=306*(mm+1)/10, %2=C+DD+E+F-1524%3
exit /B
如果您%日期%格式不是MM / DD / YYYY,重新排序毫米,DD和YY变量的第一行:DateToJDN子程序
If your %date% format is not MM/DD/YYYY, reorder mm, dd and yy variables in the first line of :DateToJDN subroutine.
这篇关于MS DOS批处理删除目录中的旧文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!