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