删除指定目录下,所有在某个指定分钟以前的文件,可以用来清理经常生成很多文件的目录,但又需要保留最新的一部分文件
支持拖放目录到界面
能够记忆设置,下次启动后不用重新设置,可以直接开始
应用场景比如:游戏定时存档,日志目录、监控文件目录等
关于这个删除后备份,我的需求是这样,如果你要的是删除前备份,可以把_del()函数里的_bak()调用放到最前面
不多废话,直接上代码
SetWorkingDir %A_ScriptDir%SplitPath, A_ScriptName,,,,IniFile
IniFile = %IniFile%.iniGui New, -0x10000 -0x30000 ;无最小化和最大坏按钮
Gui Font, s9, Segoe UIGui Add, Text, x10 y15 w34 h20 +0x200, 目录:
Gui Add, Edit, vEdtFolder gOnEdtFolderChange x64 y14 w299 h21Gui Add, Text, x10 y51 w48 h23 +0x200, 分钟前:
Gui Add, Edit, vEdtMin gOnEdtMinChange x63 y51 w26 h21Gui Add, CheckBox, vChkBak gOnChkBakChange x10 y88 w85 h23, 删除后备份Gui Add, Text, x115 y50 w98 h23 +0x200, 检测间隔[分钟]:
Gui Add, Edit, vEdtInterval gOnEdtIntervalChange x216 y50 w26 h21Gui Add, Edit, vEdtZipFile gOnEdtZipFileChange x100 y88 w265 h21Gui Add, Button, gOnBtnStart x147 y130 w72 h23, 开始监视;读取INI赋值
IniRead, Folder, % IniFile, Base, Folder
if (Folder = "" || Folder == "ERROR")Folder = 拖放文件夹到这里
IniRead, Min, % IniFile, Base, Min
if (Min = "" || Min == "ERROR")Min = 30
IniRead, Interval, % IniFile, Base, Interval
if (Interval = "" || Interval == "ERROR")Interval = 5
IniRead, IsBackup, % IniFile, Base, IsBackup
if (IsBackup = "" || IsBackup == "ERROR")IsBackup = 0
IniRead, ZipFile, % IniFile, Base, ZipFile
if (ZipFile = "" || ZipFile == "ERROR")ZipFile = 备份文件路径 [扩展名:.zip]
GuiControl,, EdtFolder, % Folder
GuiControl,, EdtMin, % Min
GuiControl,, EdtInterval, % Interval
GuiControl,, ChkBak, % IsBackup
GuiControl,, EdtZipFile, % ZipFileGui Show, w371 h169, 删除目录下指定分钟以前的文件
ReturnOnEdtFolderChange:
global Folder
GuiControlGet, Folder, , EdtFolder
;MsgBox % Folder
IniWrite % Folder, % IniFile, Base, Folder
ReturnOnEdtMinChange:
global Min
GuiControlGet, Min, , EdtMin
IniWrite % Min, % IniFile, Base, Min
ReturnOnEdtIntervalChange:
global Interval
GuiControlGet, Interval, , EdtInterval
IniWrite % Interval, % IniFile, Base, Interval
ReturnOnChkBakChange:
global IsBackup
GuiControlGet, IsBackup, , ChkBak
IniWrite % IsBackup, % IniFile, Base, IsBackup
ReturnOnEdtZipFileChange:
global ZipFile
GuiControlGet, ZipFile, , EdtZipFile
IniWrite % ZipFile, % IniFile, Base, ZipFile
ReturnGuiDropFiles:
Loop, Parse, A_GuiEvent, `n
{global IniFileFirstFile = %A_LoopField%FileGetAttrib, Attributes, % FirstFileIfInString, Attributes, D ;是目录;MsgBox This is a directory.GuiControl,, EdtFolder, % FirstFile ;自动调用onChangebreak
}
ReturnOnBtnStart:
global Folder
if !FileExist(Folder)
{MsgBox 目录不存在!return
}Gui, Hideglobal Interval
_del()
SetTimer _del, % Interval * 60 * 1000
ReturnGuiEscape:
GuiClose:ExitApp_del(){global Folder ;必须引用global MinLoop %Folder%\*{;计算文件的最后修改时间距离当前时间的分钟数TimeNow := A_NowEnvSub TimeNow, A_LoopFileTimeModified, Minutes;如果文件的最后修改时间超过了指定的分钟数,就删除该文件if (TimeNow > Min){;MsgBox %A_LoopFileFullPath%;尽可能删除到回收站,否则使用FileDeleteFileRecycle %A_LoopFileFullPath%}}_bak()
}_bak()
{global Folderglobal ZipFileIfExist %ZipFile%FileRecycle %ZipFile%;双引号路径可以处理空格Run 7z.exe a -tzip -r "%ZipFile%" "%Folder%",,Hide
}