windows 批处理文件删除超过 N 天的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:13:40  来源:igfitidea点击:

Batch file to delete files older than N days

windowsdatebatch-filefile-iocmd

提问by Kibbee

I am looking for a way to delete all files older than 7 days in a batch file. I've searched around the web, and found some examples with hundreds of lines of code, and others that required installing extra command line utilities to accomplish the task.

我正在寻找一种方法来删除批处理文件中超过 7 天的所有文件。我在网上搜索过,发现了一些包含数百行代码的示例,以及其他需要安装额外的命令行实用程序才能完成任务的示例。

Similar things can be done in BASHin just a couple lines of code. It seems that something at least remotely easy could be done for batch files in Windows. I'm looking for a solution that works in a standard Windows command prompt, without any extra utilities. Please no PowerShell or Cygwin either.

类似的事情可以在 BASH 中用几行代码完成。似乎可以对 Windows 中的批处理文件进行至少远程简单的操作。我正在寻找一种在标准 Windows 命令提示符下工作的解决方案,无需任何额外的实用程序。请不要使用 PowerShell 或 Cygwin。

回答by aku

Enjoy:

享受:

forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"

See forfilesdocumentationfor more details.

有关更多详细信息,请参阅forfiles文档

For more goodies, refer to An A-Z Index of the Windows XP command line.

有关更多内容,请参阅Windows XP 命令行的 AZ 索引

If you don't have forfilesinstalled on your machine, copy it from any Windows Server 2003to your Windows XP machine at %WinDir%\system32\. This is possible since the EXE is fully compatible between Windows Server 2003 and Windows XP.

如果您forfiles的计算机上尚未安装,请将其从任何Windows Server 2003复制到您的 Windows XP 计算机,地址为%WinDir%\system32\。这是可能的,因为 EXE 在 Windows Server 2003 和 Windows XP 之间完全兼容。

Later versions of Windows and Windows Server have it installed by default.

默认情况下,更高版本的 Windows 和 Windows Server 已安装它。

For Windows 7 and newer (including Windows 10):

对于 Windows 7 和更新版本(包括 Windows 10):

The syntax has changed a little. Therefore the updated command is:

语法略有变化。因此更新的命令是:

forfiles /p "C:\what\ever" /s /m *.* /D -<number of days> /C "cmd /c del @path"

回答by Iman

Run the following commands:

运行以下命令

ROBOCOPY C:\source C:\destination /mov /minage:7
del C:\destination /q

Move all the files (using /mov, which moves files and then deletes them as opposed to /move which moves whole filetrees which are then deleted) via robocopy to another location, and then execute a delete command on that path and you're all good.

通过 robocopy 将所有文件(使用 /mov,它移动文件然后删除它们,而不是 /move,它移动整个文件树然后被删除)到另一个位置,然后在该路径上执行一个删除命令,你就完成了好的。

Also if you have a directory with lots of data in it you can use /mirswitch

此外,如果您有一个包含大量数据的目录,则可以使用/mirswitch

回答by Jay

Ok was bored a bit and came up with this, which contains my version of a poor man's Linux epoch replacement limited for daily usage (no time retention):

Ok 有点无聊,想出了这个,其中包含我的一个穷人的 Linux epoch 替换版本,仅限日常使用(没有时间保留):

7daysclean.cmd

7daysclean.cmd

@echo off
setlocal ENABLEDELAYEDEXPANSION
set day=86400
set /a year=day*365
set /a strip=day*7
set dSource=C:\temp

call :epoch %date%
set /a slice=epoch-strip

for /f "delims=" %%f in ('dir /a-d-h-s /b /s %dSource%') do (
    call :epoch %%~tf
    if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)
)
exit /b 0

rem Args[1]: Year-Month-Day
:epoch
    setlocal ENABLEDELAYEDEXPANSION
    for /f "tokens=1,2,3 delims=-" %%d in ('echo %1') do set Years=%%d& set Months=%%e& set Days=%%f
    if "!Months:~0,1!"=="0" set Months=!Months:~1,1!
    if "!Days:~0,1!"=="0" set Days=!Days:~1,1!
    set /a Days=Days*day
    set /a _months=0
    set i=1&& for %%m in (31 28 31 30 31 30 31 31 30 31 30 31) do if !i! LSS !Months! (set /a _months=!_months! + %%m*day&& set /a i+=1)
    set /a Months=!_months!
    set /a Years=(Years-1970)*year
    set /a Epoch=Years+Months+Days
    endlocal& set Epoch=%Epoch%
    exit /b 0

USAGE

用法

set /a strip=day*7: Change 7for the number of days to keep.

set /a strip=day*7:将保留天数更改为7

set dSource=C:\temp: This is the starting directory to check for files.

set dSource=C:\temp:这是检查文件的起始目录。

NOTES

笔记

This is non-destructive code, it will display what would have happened.

这是非破坏性代码,它将显示会发生什么。

Change :

改变 :

if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)

to something like :

类似于:

if !epoch! LEQ %slice% del /f %%f

so files actually get deleted

所以文件实际上被删除了

February: is hard-coded to 28 days. Bissextile years is a hell to add, really. if someone has an idea that would not add 10 lines of code, go ahead and post so I add it to my code.

二月:硬编码为 28 天。Bissextile 年是一个地狱添加,真的。如果有人有一个不会添加 10 行代码的想法,请继续发布,以便我将其添加到我的代码中。

epoch: I did not take time into consideration, as the need is to delete files older than a certain date, taking hours/minutes would have deleted files from a day that was meant for keeping.

epoch:我没有考虑时间,因为需要删除早于某个日期的文件,花费数小时/分钟会删除原本要保留的文件。

LIMITATION

局限性

epochtakes for granted your short date format is YYYY-MM-DD. It would need to be adapted for other settings or a run-time evaluation (read sShortTime, user-bound configuration, configure proper field order in a filter and use the filter to extract the correct data from the argument).

纪元理所当然地认为您的短日期格式是 YYYY-MM-DD。它需要适应其他设置或运行时评估(读取 sShortTime、用户绑定配置、在过滤器中配置正确的字段顺序并使用过滤器从参数中提取正确的数据)。

Did I mention I hate this editor's auto-formating? it removes the blank lines and the copy-paste is a hell.

我有没有提到我讨厌这个编辑器的自动格式化?它删除了空白行,复制粘贴是一个地狱。

I hope this helps.

我希望这有帮助。

回答by Arno Jansen

forfiles /p "v:" /s /m *.* /d -3 /c "cmd /c del @path"

You should do /d -3(3 days earlier) This works fine for me. So all the complicated batches could be in the trash bin. Also forfilesdon't support UNC paths, so make a network connection to a specific drive.

你应该做/d -3(提前 3 天)这对我来说很好用。所以所有复杂的批次都可以放在垃圾箱中。同时forfiles不支持UNC路径,因此进行网络连接到特定的驱动器。

回答by e.James

Have a look at my answerto a similar question:

看看我的回答到一个类似的问题

REM del_old.bat
REM usage: del_old MM-DD-YYY
for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> FILES_TO_KEEP.TXT
for /f "tokens=*" %%a IN ('xcopy *.* /L /I /EXCLUDE:FILES_TO_KEEP.TXT null') do if exist "%%~nxa" del "%%~nxa"

This deletes files older than a given date. I'm sure it can be modified to go back seven days from the current date.

这将删除早于给定日期的文件。我确定它可以修改为从当前日期返回 7 天。

update:I notice that HerbCSO has improved on the above script. I recommend using his versioninstead.

更新:我注意到 HerbCSO 对上述脚本进行了改进。我建议改用他的版本

回答by segero

My command is

我的命令是

forfiles -p "d:\logs" -s -m*.log -d-15 -c"cmd /c del @PATH\@FILE" 

@PATH- is just path in my case, so I had to use @PATH\@FILE

@PATH- 在我的情况下只是路径,所以我不得不使用 @PATH\@FILE

also forfiles /?not working for me too, but forfiles(without "?") worked fine.

forfiles /?不会对我来说有效过,但forfiles(没有“?”)工作的罚款。

And the only question I have: how to add multiple mask (for example ".log|.bak")?

我唯一的问题是:如何添加多个掩码(例如“ .log|.bak”)?

All this regarding forfiles.exe that I downloaded here(on win XP)

所有这些关于我在这里下载的forfiles.exe (在 Win XP 上)

But if you are using Windows server forfiles.exe should be already there and it is differs from ftp version. That is why I should modify command.

但是如果你使用的是 Windows 服务器 forfiles.exe 应该已经存在并且它与 ftp 版本不同。这就是为什么我应该修改命令。

For Windows Server 2003 I'm using this command:

对于 Windows Server 2003,我使用以下命令:

forfiles -p "d:\Backup" -s -m *.log -d -15 -c "cmd /c del @PATH"

回答by Paris

Copy this code and save it as DelOldFiles.vbs.

复制此代码并将其另存为DelOldFiles.vbs.

USAGE IN CMD : cscript //nologo DelOldFiles.vbs 15

在 CMD 中的用法: cscript //nologo DelOldFiles.vbs 15

15 means to delete files older than 15 days in past.

15 表示删除过去 15 天之前的文件。

  'copy from here
    Function DeleteOlderFiles(whichfolder)
       Dim fso, f, f1, fc, n, ThresholdDate
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set f = fso.GetFolder(whichfolder)
       Set fc = f.Files
       Set objArgs = WScript.Arguments
       n = 0
       If objArgs.Count=0 Then
           howmuchdaysinpast = 0
       Else
           howmuchdaysinpast = -objArgs(0)
       End If
       ThresholdDate = DateAdd("d", howmuchdaysinpast, Date)   
       For Each f1 in fc
     If f1.DateLastModified<ThresholdDate Then
        Wscript.StdOut.WriteLine f1
        f1.Delete
        n = n + 1    
     End If
       Next
       Wscript.StdOut.WriteLine "Deleted " & n & " file(s)."
    End Function

    If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
      WScript.Echo "USAGE ONLY IN COMMAND PROMPT: cscript DelOldFiles.vbs 15" & vbCrLf & "15 means to delete files older than 15 days in past."
      WScript.Quit 0   
    End If

    DeleteOlderFiles(".")
 'to here

回答by Mofi

There are very often relative date/time related questions to solve with batch file. But command line interpreter cmd.exehas no function for date/time calculations. Lots of good working solutions using additional console applications or scripts have been posted already here, on other pages of Stack Overflow and on other websites.

经常有相对日期/时间相关的问题需要用批处理文件来解决。但是命令行解释器cmd.exe没有日期/时间计算的功能。在这里、Stack Overflow 的其他页面和其他网站上已经发布了许多使用其他控制台应用程序或脚本的良好工作解决方案。

Common for operations based on date/time is the requirement to convert a date/time string to seconds since a determined day. Very common is 1970-01-01 00:00:00 UTC. But any later day could be also used depending on the date range required to support for a specific task.

基于日期/时间的操作的常见要求是将日期/时间字符串转换为自确定日期以来的秒数。非常常见的是 1970-01-01 00:00:00 UTC。但是,根据支持特定任务所需的日期范围,也可以使用以后的任何一天。

Jayposted 7daysclean.cmdcontaining a fast "date to seconds" solution for command line interpreter cmd.exe. But it does not take leap years correct into account. J.R.posted an add-onfor taking leap day in current year into account, but ignoring the other leap years since base year, i.e. since 1970.

Jay发布了7daysclean.cmd ,其中包含命令行解释器cmd.exe的快速“日期到秒”解决方案。但它没有考虑到闰年是正确的。JR发布了一个附加组件,用于考虑当前年份的闰日,但忽略自基准年(即自 1970 年以来)的其他闰年。

I use since 20 years static tables (arrays) created once with a small C function for quickly getting the number of days including leap days from 1970-01-01 in date/time conversion functions in my applications written in C/C++.

我使用自 20 年以来用一个小的 C 函数创建一次的静态表(数组),以便在我用 C/C++ 编写的应用程序中的日期/时间转换函数中快速获取天数,包括从 1970 年 1 月 1 日起的闰日。

This very fast table method can be used also in batch code using FORcommand. So I decided to code the batch subroutine GetSecondswhich calculates the number of seconds since 1970-01-01 00:00:00 UTC for a date/time string passed to this routine.

这种非常快速的表格方法也可以使用FOR命令在批处理代码中使用。所以我决定编写批处理子例程GetSeconds,它计算自 1970-01-01 00:00:00 UTC 以来传递给该例程的日期/时间字符串的秒数。

Note:Leap seconds are not taken into account as the Windows file systems also do not support leap seconds.

注意:不考虑闰秒,因为 Windows 文件系统也不支持闰秒。

First, the tables:

首先是表:

  1. Days since 1970-01-01 00:00:00 UTC for each year including leap days.

    1970 - 1979:     0   365   730  1096  1461  1826  2191  2557  2922  3287
    1980 - 1989:  3652  4018  4383  4748  5113  5479  5844  6209  6574  6940
    1990 - 1999:  7305  7670  8035  8401  8766  9131  9496  9862 10227 10592
    2000 - 2009: 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245
    2010 - 2019: 14610 14975 15340 15706 16071 16436 16801 17167 17532 17897
    2020 - 2029: 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550
    2030 - 2039: 21915 22280 22645 23011 23376 23741 24106 24472 24837 25202
    2040 - 2049: 25567 25933 26298 26663 27028 27394 27759 28124 28489 28855
    2050 - 2059: 29220 29585 29950 30316 30681 31046 31411 31777 32142 32507
    2060 - 2069: 32872 33238 33603 33968 34333 34699 35064 35429 35794 36160
    2070 - 2079: 36525 36890 37255 37621 37986 38351 38716 39082 39447 39812
    2080 - 2089: 40177 40543 40908 41273 41638 42004 42369 42734 43099 43465
    2090 - 2099: 43830 44195 44560 44926 45291 45656 46021 46387 46752 47117
    2100 - 2106: 47482 47847 48212 48577 48942 49308 49673
    

    Calculating the seconds for year 2039 to 2106 with epoch beginning 1970-01-01 is only possible with using an unsigned 32-bit variable, i.e. unsigned long (or unsigned int) in C/C++.

    But cmd.exeuse for mathematical expressions a signed 32-bit variable. Therefore the maximum value is 2147483647 (0x7FFFFFFF) which is 2038-01-19 03:14:07.

  2. Leap year information (No/Yes) for the years 1970 to 2106.

    1970 - 1989: N N Y N N N Y N N N Y N N N Y N N N Y N
    1990 - 2009: N N Y N N N Y N N N Y N N N Y N N N Y N
    2010 - 2029: N N Y N N N Y N N N Y N N N Y N N N Y N
    2030 - 2049: N N Y N N N Y N N N Y N N N Y N N N Y N
    2050 - 2069: N N Y N N N Y N N N Y N N N Y N N N Y N
    2070 - 2089: N N Y N N N Y N N N Y N N N Y N N N Y N
    2090 - 2106: N N Y N N N Y N N N N N N N Y N N
                                     ^ year 2100
    
  3. Number of days to first day of each month in current year.

                       Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    Year with 365 days:  0  31  59  90 120 151 181 212 243 273 304 334
    Year with 366 days:  0  31  60  91 121 152 182 213 244 274 305 335
    
  1. 自 1970-01-01 00:00:00 UTC 以来每年的天数,包括闰日。

    1970 - 1979:     0   365   730  1096  1461  1826  2191  2557  2922  3287
    1980 - 1989:  3652  4018  4383  4748  5113  5479  5844  6209  6574  6940
    1990 - 1999:  7305  7670  8035  8401  8766  9131  9496  9862 10227 10592
    2000 - 2009: 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245
    2010 - 2019: 14610 14975 15340 15706 16071 16436 16801 17167 17532 17897
    2020 - 2029: 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550
    2030 - 2039: 21915 22280 22645 23011 23376 23741 24106 24472 24837 25202
    2040 - 2049: 25567 25933 26298 26663 27028 27394 27759 28124 28489 28855
    2050 - 2059: 29220 29585 29950 30316 30681 31046 31411 31777 32142 32507
    2060 - 2069: 32872 33238 33603 33968 34333 34699 35064 35429 35794 36160
    2070 - 2079: 36525 36890 37255 37621 37986 38351 38716 39082 39447 39812
    2080 - 2089: 40177 40543 40908 41273 41638 42004 42369 42734 43099 43465
    2090 - 2099: 43830 44195 44560 44926 45291 45656 46021 46387 46752 47117
    2100 - 2106: 47482 47847 48212 48577 48942 49308 49673
    

    计算从 1970-01-01 开始的纪元 2039 年到 2106 年的秒数只能使用无符号 32 位变量,即 C/C++ 中的 unsigned long(或 unsigned int)。

    但是cmd.exe用于数学表达式的有符号 32 位变量。因此最大值是 2147483647 (0x7FFFFFFF),即 2038-01-19 03:14:07。

  2. 1970 年至 2106 年的闰年信息(否/是)。

    1970 - 1989: N N Y N N N Y N N N Y N N N Y N N N Y N
    1990 - 2009: N N Y N N N Y N N N Y N N N Y N N N Y N
    2010 - 2029: N N Y N N N Y N N N Y N N N Y N N N Y N
    2030 - 2049: N N Y N N N Y N N N Y N N N Y N N N Y N
    2050 - 2069: N N Y N N N Y N N N Y N N N Y N N N Y N
    2070 - 2089: N N Y N N N Y N N N Y N N N Y N N N Y N
    2090 - 2106: N N Y N N N Y N N N N N N N Y N N
                                     ^ year 2100
    
  3. 到当年每个月的第一天的天数。

                       Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    Year with 365 days:  0  31  59  90 120 151 181 212 243 273 304 334
    Year with 366 days:  0  31  60  91 121 152 182 213 244 274 305 335
    

Converting a date to number of seconds since 1970-01-01 is quite easy using those tables.

使用这些表很容易将日期转换为自 1970-01-01 以来的秒数。

Attention please!

请注意!

The format of date and time strings depends on Windows region and language settings. The delimiters and the order of tokens assigned to the environment variables Day, Monthand Yearin first FORloop of GetSecondsmust be adapted to local date/time format if necessary.

日期和时间字符串的格式取决于 Windows 区域和语言设置。分隔符和分配给环境变量令牌的顺序DayMonthYear在第一FOR的循环GetSeconds如果需要的话,必须适应当地的日期/时间格式。

It is necessary to adapt the date string of the environment variable if date format in environment variable DATEis different to date format used by command FORon %%~tF.

如果环境变量DATE中的日期格式与FORon命令使用的日期格式不同,则需要调整环境变量的日期字符串%%~tF

For example when %DATE%expands to Sun 02/08/2015while %%~tFexpands to 02/08/2015 07:38 PMthe code below can be used with modifying line 4 to:

例如,当%DATE%扩展为Sun 02/08/2015while%%~tF扩展为02/08/2015 07:38 PM下面的代码时,可以将第 4 行修改为:

call :GetSeconds "%DATE:~4% %TIME%"

This results in passing to subroutine just 02/08/2015- the date string without the 3 letters of weekday abbreviation and the separating space character.

这导致只传递给子程序02/08/2015- 没有工作日缩写的 3 个字母和分隔空格字符的日期字符串。

Alternatively following could be used to pass current date in correct format:

或者,以下可用于以正确的格式传递当前日期:

call :GetSeconds "%DATE:~-10% %TIME%"

Now the last 10 characters from date string are passed to function GetSecondsand therefore it does not matter if date string of environment variable DATEis with or without weekday as long as day and month are always with 2 digits in expected order, i.e. in format dd/mm/yyyyor dd.mm.yyyy.

现在日期字符串中的最后 10 个字符被传递给函数GetSeconds,因此环境变量DATE 的日期字符串是否包含工作日都没有关系,只要日期和月份始终按预期顺序使用 2 位数字,即格式dd/mm/yyyydd.mm.yyyy.

Here is the batch code with explaining comments which just outputs which file to delete and which file to keep in C:\Tempfolder tree, see code of first FORloop.

这是带有解释注释的批处理代码,它只输出要删除的文件和要保留在C:\Temp文件夹树中的文件,请参阅第一个FOR循环的代码。

@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Get seconds since 1970-01-01 for current date and time.
call :GetSeconds "%DATE% %TIME%"
rem Subtract seconds for 7 days from seconds value.
set /A "LastWeek=Seconds-7*86400"

rem For each file in each subdirectory of C:\Temp get last modification date
rem (without seconds -> append second 0) and determine the number of seconds
rem since 1970-01-01 for this date/time. The file can be deleted if seconds
rem value is lower than the value calculated above.

for /F "delims=" %%F in ('dir /A-D-H-S /B /S "C:\Temp"') do (
    call :GetSeconds "%%~tF:0"
    rem if !Seconds! LSS %LastWeek% del /F "%%~fF"
    if !Seconds! LEQ %LastWeek% (
        echo Delete "%%~fF"
    ) else (
        echo Keep   "%%~fF"
    )
)
endlocal
goto :EOF


rem No validation is made for best performance. So make sure that date
rem and hour in string is in a format supported by the code below like
rem MM/DD/YYYY hh:mm:ss or M/D/YYYY h:m:s for English US date/time.

:GetSeconds

rem If there is " AM" or " PM" in time string because of using 12 hour
rem time format, remove those 2 strings and in case of " PM" remember
rem that 12 hours must be added to the hour depending on hour value.

set "DateTime=%~1"
set "Add12Hours=0"
if "%DateTime: AM=%" NEQ "%DateTime%" (
    set "DateTime=%DateTime: AM=%"
) else if "%DateTime: PM=%" NEQ "%DateTime%" (
    set "DateTime=%DateTime: PM=%"
    set "Add12Hours=1"
)

rem Get year, month, day, hour, minute and second from first parameter.

for /F "tokens=1-6 delims=,-./: " %%A in ("%DateTime%") do (
    rem For English US date MM/DD/YYYY or M/D/YYYY
    set "Day=%%B" & set "Month=%%A" & set "Year=%%C"
    rem For German date DD.MM.YYYY or English UK date DD/MM/YYYY
    rem set "Day=%%A" & set "Month=%%B" & set "Year=%%C"
    set "Hour=%%D" & set "Minute=%%E" & set "Second=%%F"
)
rem echo Date/time is: %Year%-%Month%-%Day% %Hour%:%Minute%:%Second%

rem Remove leading zeros from the date/time values or calculation could be wrong.
if "%Month:~0,1%"  EQU "0" ( if "%Month:~1%"  NEQ "" set "Month=%Month:~1%"   )
if "%Day:~0,1%"    EQU "0" ( if "%Day:~1%"    NEQ "" set "Day=%Day:~1%"       )
if "%Hour:~0,1%"   EQU "0" ( if "%Hour:~1%"   NEQ "" set "Hour=%Hour:~1%"     )
if "%Minute:~0,1%" EQU "0" ( if "%Minute:~1%" NEQ "" set "Minute=%Minute:~1%" )
if "%Second:~0,1%" EQU "0" ( if "%Second:~1%" NEQ "" set "Second=%Second:~1%" )

rem Add 12 hours for time range 01:00:00 PM to 11:59:59 PM,
rem but keep the hour as is for 12:00:00 PM to 12:59:59 PM.
if "%Add12Hours%" == "1" (
    if %Hour% LSS 12 set /A Hour+=12
)
set "DateTime="
set "Add12Hours="

rem Must use 2 arrays as more than 31 tokens are not supported
rem by command line interpreter cmd.exe respectively command FOR.
set /A "Index1=Year-1979"
set /A "Index2=Index1-30"

if %Index1% LEQ 30 (
    rem Get number of days to year for the years 1980 to 2009.
    for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
    for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
) else (
    rem Get number of days to year for the years 2010 to 2038.
    for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
    for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
)

rem Add the days to month in year.
if "%LeapYear%" == "N" (
    for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
    for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)

rem Add the complete days in month of year.
set /A "Days+=Day-1"

rem Calculate the seconds which is easy now.
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"

rem Exit this subroutine
goto :EOF

For optimal performance it would be best to remove all comments, i.e. all lines starting with remafter 0-4 leading spaces.

为了获得最佳性能,最好删除所有注释,即在 0-4 个前导空格后以rem开头的所有行。

And the arrays can be made also smaller, i.e. decreasing the time range from 1980-01-01 00:00:00 to 2038-01-19 03:14:07 as currently supported by the batch code above for example to 2015-01-01 to 2019-12-31 as the code below uses which really deletes files older than 7 days in C:\Tempfolder tree.

并且数组也可以更小,即将时间范围从 1980-01-01 00:00:00 减少到 2038-01-19 03:14:07 ,例如上面的批处理代码目前支持到 2015-01 -01 到 2019-12-31 作为下面的代码使用它真正删除文件C:\Temp夹树中超过 7 天的文件。

Further the batch code below is optimized for 24 hours time format.

此外,下面的批处理代码针对 24 小时时间格式进行了优化。

@echo off
setlocal EnableDelayedExpansion
call :GetSeconds "%DATE:~-10% %TIME%"
set /A "LastWeek=Seconds-7*86400"

for /F "delims=" %%F in ('dir /A-D-H-S /B /S "C:\Temp"') do (
    call :GetSeconds "%%~tF:0"
    if !Seconds! LSS %LastWeek% del /F "%%~fF"
)
endlocal
goto :EOF

:GetSeconds
for /F "tokens=1-6 delims=,-./: " %%A in ("%~1") do (
    set "Day=%%B" & set "Month=%%A" & set "Year=%%C"
    set "Hour=%%D" & set "Minute=%%E" & set "Second=%%F"
)
if "%Month:~0,1%"  EQU "0" ( if "%Month:~1%"  NEQ "" set "Month=%Month:~1%"   )
if "%Day:~0,1%"    EQU "0" ( if "%Day:~1%"    NEQ "" set "Day=%Day:~1%"       )
if "%Hour:~0,1%"   EQU "0" ( if "%Hour:~1%"   NEQ "" set "Hour=%Hour:~1%"     )
if "%Minute:~0,1%" EQU "0" ( if "%Minute:~1%" NEQ "" set "Minute=%Minute:~1%" )
if "%Second:~0,1%" EQU "0" ( if "%Second:~1%" NEQ "" set "Second=%Second:~1%" )
set /A "Index=Year-2014"
for /F "tokens=%Index% delims= " %%Y in ("16436 16801 17167 17532 17897") do set "Days=%%Y"
for /F "tokens=%Index% delims= " %%L in ("N Y N N N") do set "LeapYear=%%L"
if "%LeapYear%" == "N" (
    for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
    for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)
set /A "Days+=Day-1"
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"
goto :EOF

For even more information about date and time formats and file time comparisons on Windows see my answer on Find out if file is older than 4 hours in batch filewith lots of additional information about file times.

有关 Windows 上的日期和时间格式以及文件时间比较的更多信息,请参阅我的回答关于找出批处理文件中的文件是否超过 4 小时以及有关文件时间的许多附加信息。

回答by NotJustClarkKent

For Windows Server 2008 R2:

对于 Windows Server 2008 R2:

forfiles /P c:\sql_backups\ /S /M *.sql /D -90 /C "cmd /c del @PATH"

This will delete all .sqlfiles older than 90days.

这将删除超过90天的所有.sql文件。

回答by Aidan Ewen

Use forfiles.

使用 forfiles。

There are different versions. Early ones use unix style parameters.

有不同的版本。早期的使用 unix 样式参数。

My version (for server 2000 - note no space after switches)-

我的版本(对于服务器 2000 - 注意开关后没有空格)-

forfiles -p"C:\what\ever" -s -m*.* -d<number of days> -c"cmd /c del @path"

To add forfiles to XP, get the exe from ftp://ftp.microsoft.com/ResKit/y2kfix/x86/

要将 forfiles 添加到 XP,请从ftp://ftp.microsoft.com/ResKit/y2kfix/x86/获取 exe

and add it to C:\WINDOWS\system32

并将其添加到 C:\WINDOWS\system32