AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 66698
Accepted
jjrab
jjrab
Asked: 2009-09-19 05:48:45 +0800 CST2009-09-19 05:48:45 +0800 CST 2009-09-19 05:48:45 +0800 CST

根据文件年龄自动删除文件

  • 772

有谁知道根据年龄删除文件并定期删除文件的好方法,比如使用批处理文件和 Windows 调度程序?(或任何其他可以做到这一点的体面软件)

windows
  • 4 4 个回答
  • 5095 Views

4 个回答

  • Voted
  1. Best Answer
    dogmanky
    2009-09-19T06:14:05+08:002009-09-19T06:14:05+08:00

    +1 为“forfiles”

    这是我在每晚运行的批处理文件中使用的内容:

    forfiles -pE:\myfolder\mysubfolder -m*.* -d-5 -c"cmd /C del /q @FILE"

    • 6
  2. Igal Serban
    2009-09-19T05:58:44+08:002009-09-19T05:58:44+08:00

    您可以使用forfiles 命令。直接用windows scheduler来调度就行了。

    还有一些例子。

    • 3
  3. dubRun
    2009-09-19T05:55:57+08:002009-09-19T05:55:57+08:00

    是的,我有一个我们用的。网上找的,做了一些修改。

    这将作为

    删除文件.bat 5 C:\Temp *.LOG 1

    并将删除 C:\Temp 及其子目录中超过 5 天的所有日志文件

    啊,找到原帖了

    @echo off
    
    CLS
    :HouseKeeping
    REM Start Time
    REM This will load the current time into 'Start' variables.
    REM.
    Set Start_Time=%time%
    FOR /f "tokens=1-3 delims=:." %%a in ('echo %time%') do (
    set Start_hh=%%a
    set Start_mm=%%b
    set Start_ss=%%c
    )
    
    FOR /f "tokens=1-3 delims=: " %%a in ('time /t') do (set Start_Meridan=%%c)
    Echo Start Time: %Start_hh%:%Start_mm%:%Start_ss% %Start_Meridan%
    REM Echo Start Time: %Start_hh%:%Start_mm%:%Start_ss%
    
    Set Delete_Counter=0
    Set FChecked_Counter=0
    Set Total_Files=0
    Set Finish_day=0
    SET OLDERTHAN=%1
    SET FileDir=%~2
    SET EXT=%3
    SET Sub_Dir=%4
    SET Force_Check=%5
    IF NOT DEFINED OLDERTHAN GOTO SYNTAX
    IF NOT DEFINED FileDir GOTO SYNTAX
    IF NOT DEFINED EXT GOTO SYNTAX
    IF NOT DEFINED Force_Check Goto Get_Date
    IF DEFINED Force_Check Goto Read_Only
    Goto Exit
    
    :Read_Only
    IF %Force_Check%==1 set Force_Delete=/F
    Goto Get_Date
    
    REM Get Todays Date
    :Get_Date
    FOR /f "tokens=1-4 delims=/- " %%a in ('date /t') do (
    set xx=%%a
    set mm=%%b
    set dd=%%c
    set yyyy=%%d
    )
    
    set /A mm=%mm%+0
    if %dd%==01 set dd=1
    if %dd%==02 set dd=2
    if %dd%==03 set dd=3
    if %dd%==04 set dd=4
    if %dd%==05 set dd=5
    if %dd%==06 set dd=6
    if %dd%==07 set dd=7
    if %dd%==08 set dd=8
    if %dd%==09 set dd=9
    set /A dd=%dd%+0
    set /A dd=%dd% - %OLDERTHAN%
    set /A yyyy=%yyyy%+0
    :LOOPDATE
    
    if /I %dd% GTR 0 Goto DONE
    set /A mm=%mm% - 1
    if /I %mm% GTR 0 Goto ADJUSTDAY
    set /A mm=12
    set /A yyyy=%yyyy% - 1
    :ADJUSTDAY
    if %mm%==1 Goto SET31
    if %mm%==2 Goto LEAPCHK
    if %mm%==3 Goto SET31
    if %mm%==4 Goto SET30
    if %mm%==5 Goto SET31
    if %mm%==6 Goto SET30
    if %mm%==7 Goto SET31
    if %mm%==8 Goto SET31
    if %mm%==9 Goto SET30
    if %mm%==10 Goto SET31
    if %mm%==11 Goto SET30
    if %mm%==12 Goto SET31
    :SET31
    set /A dd=31 + %dd%
    Goto LOOPDATE
    
    :SET30
    set /A dd=30 + %dd%
    Goto LOOPDATE
    
    :LEAPCHK
    set /A tt=%yyyy% %% 4
    if not %tt%==0 Goto SET28
    set /A tt=%yyyy% %% 100
    if not %tt%==0 Goto SET29
    set /A tt=%yyyy% %% 400
    if %tt%==0 Goto SET29
    
    :SET28
    set /A dd=28 + %dd%
    Goto LOOPDATE
    
    :SET29
    set /A dd=29 + %dd%
    
    :DONE
    if /i %dd% LSS 10 set dd=0%dd%
    rem if /I %mm% LSS 10 set mm=0%mm%
    
    :Search_Type
    IF NOT DEFINED Sub_Dir GOTO No_Sub_Dir
    If %Sub_Dir%==0 Goto :No_Sub_Dir
    If %Sub_Dir%==1 Goto :Sub_Directory
    Else Goto :No_Sub_Dir
    
    :No_Sub_Dir
    for %%i in (%FileDir%\%EXT%) do (
    set FileName=%%i
    call :PROCESSFILE %%~ti)
    
    Goto Print_Results
    
    :Sub_Directory
    for /R %FileDir% %%i in (%EXT%) DO (
    set FileName=%%i
    call :PROCESSFILE %%~ti)
    Goto Print_Results
    
    :Print_Results
    Echo.
    Echo The number of files deleted : %Delete_Counter%
    Echo.
    Echo The number of files passed over : %FChecked_Counter%
    Echo.
    Echo The Total number of files processed: %Total_Files%
    Echo.
    Goto Get_Finish_Time
    
    :Get_Finish_Time
    REM This will load the current time into 'Finish' variables.
    REM.
    FOR /f "tokens=1-3 delims=:." %%a in ('echo %time%') do (
    set Finish_hh=%%a
    set Finish_mm=%%b
    set Finish_ss=%%c
    )
    FOR /f "tokens=1-3 delims=: " %%a in ('time /t') do (set Finish_Meridan=%%c)
    Goto Calculate_Time
    
    :Calculate_Time
    REM Calculate the difference in time between Start and Finish times
    REM.
    
    IF %Start_ss% GTR %Finish_ss% (
    set /A Finish_ss=%Finish_ss%+60
    set /A Finish_mm=%Finish_mm%-1
    )
    set /A Diff_ss=%Finish_ss%-%Start_ss%
    
    IF %Start_mm% GTR %Finish_mm% (
    set /A Finish_mm=%Finish_mm%+60
    set /A Finish_hh=%Finish_hh%-1
    )
    set /A Diff_mm=%Finish_mm%-%Start_mm%
    
    IF %Start_hh% GTR %Finish_hh% (
    set /A Finish_hh=%Finish_hh%+24
    set Finish_day=1
    )
    set /A Diff_hh=%Finish_hh%-%Start_hh%
    
    REM The follow will adjust for negative time values.
    REM.
    REM if %Diff_hh% LSS 0 (set /A Diff_hh=%Diff_hh%+23)
    REM if %Diff_mm% LSS 0 (set /A Diff_mm=%Diff_mm%+59)
    REM if %Diff_ss% LSS 0 (set /A Diff_ss=%Diff_ss%+59)
    REM The following will compensate for single digit results.
    REM.
    if %Diff_hh%==0 set Diff_hh=00
    if %Diff_hh%==1 set Diff_hh=01
    if %Diff_hh%==2 set Diff_hh=02
    if %Diff_hh%==3 set Diff_hh=03
    if %Diff_hh%==4 set Diff_hh=04
    if %Diff_hh%==5 set Diff_hh=05
    if %Diff_hh%==6 set Diff_hh=06
    if %Diff_hh%==7 set Diff_hh=07
    if %Diff_hh%==8 set Diff_hh=08
    if %Diff_hh%==9 set Diff_hh=09
    if %Diff_mm%==0 set Diff_mm=00
    if %Diff_mm%==1 set Diff_mm=01
    if %Diff_mm%==2 set Diff_mm=02
    if %Diff_mm%==3 set Diff_mm=03
    if %Diff_mm%==4 set Diff_mm=04
    if %Diff_mm%==5 set Diff_mm=05
    if %Diff_mm%==6 set Diff_mm=06
    if %Diff_mm%==7 set Diff_mm=07
    if %Diff_mm%==8 set Diff_mm=08
    if %Diff_mm%==9 set Diff_mm=09
    if %Diff_ss%==0 set Diff_ss=00
    if %Diff_ss%==1 set Diff_ss=01
    if %Diff_ss%==2 set Diff_ss=02
    if %Diff_ss%==3 set Diff_ss=03
    if %Diff_ss%==4 set Diff_ss=04
    if %Diff_ss%==5 set Diff_ss=05
    if %Diff_ss%==6 set Diff_ss=06
    if %Diff_ss%==7 set Diff_ss=07
    if %Diff_ss%==8 set Diff_ss=08
    if %Diff_ss%==9 set Diff_ss=09
    Goto Print_Time
    
    :Print_Time
    REM This will return the results to the user.
    REM.
    Set End_Time=%time%
    Echo.
    Echo The program started at: %Start_Time% %Start_Meridan%
    Echo The program ended at : %End_Time% %Finish_Meridan%
    Echo hh:mm:ss.mm
    Echo.
    Echo This is the amount of time elapsed from the
    Echo start to the end of the program executing.
    Echo.
    Echo %Diff_hh%:%Diff_mm%:%Diff_ss%
    Echo hh:mm:ss
    Echo. Number of Days Elapsed: %Finish_day%
    
    Goto Reset_Variables
    
    :Reset_Variables
    
    REM The following will reset the variables used by this program.
    REM A.K.A. cleaning up.
    set mm=
    set yyyy=
    set dd=
    set thedate=
    set xx=
    set tt=
    set FileDir=
    set FileName=
    set ext=
    set OlderThan=
    set Sub_Dir=
    set Delete_Counter=
    set Total_Files=
    set FChecked_Counter=
    set Force_Check=
    set Force_Delete=
    set Diff_hh=
    set Diff_mm=
    set Diff_ss=
    set Start_hh=
    set Start_mm=
    set Start_ss=
    set Start_Meridan=
    set Finish_hh=
    set Finish_mm=
    set Finish_ss=
    set Finish_Meridan=
    set mm=
    set hh=
    set ss=
    set Start_Time=
    set End_Time=
    set Finish_day=
    Goto Exit
    
    
    GOTO Exit
    
    :PROCESSFILE
    set temp=%1
    set fyyyy=%temp:~6%
    
    if /I %fyyyy% LSS 100 set fyyyy=20%fyyyy%
    if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
    
    set fmm=%temp:~0,2%
    set fdd=%temp:~3,2%
    
    :: +*************************************+
    :: | This is where the files are deleted |
    :: | Change the DIR command to DEL to |
    :: | delete. DIR is used for test. |
    :: +*************************************+
    
    if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (
    rem Echo %yyyy%/%mm%/%dd% "- The selected file will be deleted.  F: " %fyyyy%/%fmm%/%fdd% %FileName% >> files.txt
    echo %FileName% >> d:\deletelog%yyyy%-%mm%-%dd%.txt
    del %FileName% >> d:\deletelog%yyyy%-%mm%-%dd%.txt
    rem Dir %Force_Delete% "%FileName%"
    Set /A Delete_Counter=%Delete_Counter%+1
    Set /A Total_Files=%Total_Files%+1
    Echo.
    ) Else (
    rem Echo %yyyy%/%mm%/%dd% " - The selected file does not qualify for deletion. F: " %fyyyy%/%fmm%/%fdd% %FileName%  "%FileName%" >> files.txt
    Set /A FChecked_Counter=%FChecked_Counter%+1
    Set /A Total_Files=%Total_Files%+1
    Echo.) 
    
    set temp=
    set fyyyy=
    set fmm=
    set fdd=
    
    :EXIT
    
    • 1
  4. squillman
    2009-09-19T06:02:52+08:002009-09-19T06:02:52+08:00

    这是您可以在计划任务中使用的 Powershell 脚本。(也在这个答案中使用它来清除 IIS 日志文件)。

    set-location [enter-path-to-folder]
    foreach ($File in get-childitem) {
       if ($File.LastWriteTime -lt (Get-Date).AddDays(-30)) {
          del $File
       }
    }
    

    将文件夹路径更改为您的目标,根据需要调整 AddDays。-30 删除超过 30 天的内容。

    • 1

相关问题

  • 知道任何适用于 Windows 的快速可编写脚本的 ftp 客户端吗?[关闭]

  • 如果 Windows 服务崩溃,如何自动重新启动它?

  • 如何在笔记本电脑上使用 Tobit David?[关闭]

  • 无法安排任务(访问被拒绝)

  • 物理机重启时自动重启虚拟机(VMWare)

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve