根据文件名中的日期删除文件夹中的所有文件

根据文件名中的日期删除文件夹中的所有文件

我正在尝试弄清楚如何删除文件夹中超过两天的所有文件。上次修改日期没有帮助。我需要按照文件名中的日期进行操作。文件名将类似于:AD-YYMMDD-B7D 或 BB-YYMMDD-A6C 或 RE-YYMMDD-A13T。这很困难,因为文件名的长度并不总是相同的。我发现这个脚本似乎会有所帮助,但我不知道如何更改变量以及如何设置路径。

   @ECHO OFF
ECHO Delete By Date Pattern
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.

REM Delete/Select files based on a date which utilizes MM and/or DD for file naming patterns.
REM
REM Usage:
REM DeleteByDatePattern {/M | /D} NumberToKeep Path PatternPrefix PatternPostfix [/L | /DEL]
REM     /M     Specifies the pattern being used is based on months.
REM     /D     Specifies the pattern being used is based on days.
REM     NumberToKeep
REM            The number of months (/M) or days (/D) to keep, including the current.
REM            For example, entering 1 keeps only the current month/day and 6 would keep the current minus 5.
REM     Path   The root location to search. Subdirectories will be searched.
REM     PatternPrefix
REM            The file search pattern placed before of the month/day when building the search string.
REM     PatternPostfix
REM            The file search pattern placed after of the month/day when building the search string.
REM     /L     (optional) Lists all files matching the pattern, but does not delete them.
REM     /DEL   (optional) Deletes all files matching the pattern.
REM
REM Examples:
REM     DeleteByDatePattern /M 3 "%WinDir%\system32\LogFiles" ex?? ??.log /DEL
REM         Deletes all IIS log files (Windows Server 2003) except for the current and previous two months.
REM     DeleteByDatePattern /D 7 "D:\Backup" *-????-??- .zip /DEL
REM         Deletes all zip files from the D:\Backup folder except for the current week.
REM         The file name pattern assumed above is "*-YYYY-MM-DD.zip"
REM     DeleteByDatePattern /M 0 "C:\" *( )* /L
REM         Prints a list of all files on the C drive matching the pattern: "*-MM-*" (where MM is replaced with 01-12)
REM     DeleteByDatePattern /D 14 "C:\Logs" Log-???? .txt
REM         Prints a list of all patterns which would be processed by the script.

SETLOCAL EnableExtensions EnableDelayedExpansion

REM Assumes your Windows Date/Time settings are set to 'DayOfWeek M/D/YYYY' format.
REM If your format is different, you will need to alter the variables below so they align.
FOR /F "tokens=1,2,3,4 delims=/ " %%A IN ('DATE /T') DO (
    SET Month=%%B
    SET Day=%%C
    SET Year=%%D
)

IF /I {%1}=={/M} (
    SET Keep=%Month%
    SET Max=12
)
IF /I {%1}=={/D} (
    SET Keep=%Day%
    SET Max=31
    REM Working off of the previous month's max days.
    SET /A PrevMonth=%Month%-1
    IF !PrevMonth! EQU 2 (
        SET Max=28
        REM Leap years... add more as needed.
        IF /I %Year% EQU 2012 SET Max=29
        IF /I %Year% EQU 2016 SET Max=29
    )
    IF /I !PrevMonth! EQU 4 SET Max=30
    IF /I !PrevMonth! EQU 6 SET Max=30
    IF /I !PrevMonth! EQU 9 SET Max=30
    IF /I !PrevMonth! EQU 11 SET Max=30
)
SET Current=%Keep%
SET /A Keep=%Keep%-%2+1

REM Determine the range to be removed.
SET /A RemoveHighStart=%Current%+1
IF /I %Keep% LSS 1 (
    SET RemoveLow=0
    SET /A RemoveHighEnd=%Keep%+%Max%-1
) ELSE (
    SET /A RemoveLow=%Keep%-1
    SET RemoveHighEnd=%Max%
)

REM Process all less than the low range.
FOR /L %%Z IN (1,1,%RemoveLow%) DO CALL :Process %%Z %3 %4 %5 %6
REM Process all greater than the high range.
FOR /L %%Z IN (%RemoveHighStart%,1,%RemoveHighEnd%) DO CALL :Process %%Z %3 %4 %5 %6

ENDLOCAL
GOTO End


:Process
SET Key=0%1
SET Key=%Key:~-2%
SET Target="%~2\%~3%Key%%~4"
ECHO Target Pattern: %Target%

IF /I {%5}=={/L} DIR %Target% /B /S
IF /I {%5}=={/DEL} DEL /F /S /Q %Target%
GOTO End

:End

答案1

下面的批处理文件假设:

  • 所有文件的名称中都有日期,格式为:XX-YYMMDD-XX.ext
  • 年份是以 2000 为基础的 2 位数字(例如,2012 年为 12)
  • 您所在地区的日期格式为 MM/DD/YYYY
  • 第一个参数给出了删除旧文件的天数(默认值为 2)

@echo off
setlocal EnableDelayedExpansion
rem First parameter indicate number of days (default=2)
set days=2
if "%1" neq "" set days=%1

rem Get Julian Day Number from todays date
for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
   set /A m=1%%a %% 100, d=1%%b %% 100, set y=%%c
)
set /A A=(m-14)/12, todayJDN = (1461*(y+4800+A))/4 + (367*(m-2-12*A))/12 - (3*((y+4900+A)/100))/4 + d - 32075

rem Process all files
for %%a in (*.*) do (
   rem Get date from file name (second token separated by hypen)
   for /F "tokens=2 delims=-" %%b in ("%%a") do set fileDate=%%b
   rem Get Julian Day Number from file date
   set y=20!fileDate:~0,2!
   set m=1!fileDate:~2,2! %% 100
   set d=1!fileDate:~4,2! %% 100
   set /A A=(m-14)/12, fileJDN = (1461*(y+4800+A))/4 + (367*(m-2-12*A))/12 - (3*((y+4900+A)/100))/4 + d - 32075
   rem Remove files older than given days
   set /A daysOlder=todayJDN - fileJDN
   if !daysOlder! gtr %days% ECHO rem "%%a"
)

首先测试程序并检查结果,然后删除最后一行的 ECHO 命令以真正删除文件。

儒略日数描述如下:http://www.hermetic.ch/cal_stud/jdn.htm

相关内容