📄 weihu.~pas
字号:
unit weihu;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ShellApi,ExtCtrls, AppEvnts;
type
TWhForm = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
startFile='start'; //该文件存在,进行维护,否则不进行维护
configFile='config.txt'; //配置文件
anyDir=$17; //任何文件夹,包括系统、隐藏和只读。
anyFile=$27; //任何文件,包括系统、隐藏和只读。
delFileMark='<DF>'; //删除文件标志
delDirMark='<DD>'; //删除文件夹标志
runFileMark='<RF>'; //运行程序标志
formatDiskMark='<FD>';//格式化标志
var
WhForm: TWhForm;
exePath:string; //维护程序所在路径
implementation
{$R *.dfm}
const
maxListlines=100; //最多保留行数
//格式化磁盘(外部函数,来自Windows操作系统的shell32.dll)
function SHFormatDrive(handle:HWND;drive,fmtID,options:word): Integer; stdcall;
external 'shell32.dll' name 'SHFormatDrive';
//删除由path指定的文件夹(包括其下的所有子文件夹及文件,即整个目录树)
procedure delDirTree(path:string);
var
sr:TSearchRec;
found:boolean;
begin
if (path[length(path)]='\') or (path[length(path)]=':')
then exit; //是根目录,则退出
found:=(findfirst(path+'\*.*',anyFile,sr)=0);
while found do
begin
SysUtils.FileSetAttr(path+'\'+sr.name,$20); //去掉只读,隐藏等属性
DeleteFile(path+'\'+sr.name);
found:=(findnext(sr)=0);
end;
found:=(findfirst(path+'\*.*',anyDir,sr)=0);
while found do
begin
if (sr.name[1]<>'.') then //是文件夹
begin
delDirTree(path+'\'+sr.name) //递归调用
end;
found:=(findnext(sr)=0);
end;
RemoveDir(path);
end;
//删除一个与指定串对象strs中各字符串相匹配的文件(fullFileName指定)。
procedure delOneFile(fullFileName:string;strs:Tstrings);
var
str1,str2:string;
i:integer;
begin
WhForm.Memo2.Clear;
WhForm.Memo2.Lines.Add(fullFileName);
while WhForm.Memo1.Lines.Count>=maxListlines do
WhForm.Memo1.Lines.Delete(0);
for i :=0 to strs.Count-1 do
begin
str1:=strs.Strings[i];
str2:=ExtractFileName(fullFileName);//从路径文件名中提取文件名
if str1[1]='*' then //文件名使用配符"*"
begin
str1:=ExtractFileExt(str1);//从文件名中提取扩展名
str2:=ExtractFileExt(str2);
end;
if str1=str2 then //文件名相匹配,则删除
begin
WhForm.Memo1.Lines.Append('delete '+fullFileName);
SysUtils.FileSetAttr(fullFileName,$20);
if not deletefile(fullFileName) then WhForm.Memo1.Lines.Append('delete '+fullFileName+' error!');
end;
end; {for}
end;
//删除由path指定的文件夹(包括其下的子文件夹)中所有与指定串对象strs中各字符串相匹配的文件。
procedure delAllFiles(path:string;strs:Tstrings);
var
sr:TSearchRec;
found:boolean;
begin
found:=(findfirst(path+'\*.*',anyFile,sr)=0);
while found do
begin
delOneFile(path+'\'+sr.name,strs);
found:=(findnext(sr)=0);
end;
found:=(findfirst(path+'\*.*',anyDir,sr)=0);
while found do
begin
if (sr.name[1]<>'.') then //是文件夹
begin
delAllFiles(path+'\'+sr.name,strs) //递归调用
end;
found:=(findnext(sr)=0);
end;
end;
//程序运行时自动进行各项维护
procedure TWhForm.FormActivate(Sender: TObject);
var
i,len:integer;
oneStr:string;
start:boolean;
tmpList,DFlist,DDlist,RFlist: TStrings;
DiskChar:char;
begin
memo1.Clear;
memo2.Clear;
Repaint;
exePath:=extractFilePath(application.ExeName);
start:=fileexists(exepath+startFile) and fileexists(exepath+configFile);
if start then //装入配置文件
begin
tmpList:=TStringList.Create;
tmpList.LoadFromFile(configFile);
if tmpList.Count<=1 then //配置文件内容为空,则退出
begin
tmpList.Free;
start:=false;
end;
end;
if start then
begin
diskChar:=upcase(tmpList.Strings[0][1]);
if tmpList.Strings[1]=formatDiskMark then //调用格式化函数
begin
SHFormatDrive(handle,ord(diskChar)-ord('A'),$ffff,0);
start:=false;
end;
end;
if start then //if1
begin
DFList:=TStringList.Create;
DDList:=TStringList.Create;
RFList:=TStringList.Create;
for i:=0 to tmpList.Count-1 do
begin
len:=length(delFileMark);
if copy(tmpList.Strings[i],1,len)=delFileMark then //将要删除的文件项加入DFList串对象中
DFlist.Append(copy(tmpList.Strings[i],len+1,
length(tmpList.Strings[i])-len));
len:=length(delDirMark);
if copy(tmpList.Strings[i],1,len)=delDirMark then//将要删除的文件夹项加入DDList串对象中
DDlist.Append(copy(tmpList.Strings[i],len+1,
length(tmpList.Strings[i])-len));
len:=length(runFileMark);
if copy(tmpList.Strings[i],1,len)=runFileMark then//将要运行的程序项加入RFList串对象中
RFlist.Append(copy(tmpList.Strings[i],len+1,
length(tmpList.Strings[i])-len));
end;{for}
tmpList.Free;
if DFlist.Count>0 then //删除文件
begin
delAllFiles(diskChar+':',DFList);
end;
for i :=0 to DDList.Count-1 do //删除文件夹
begin
onestr:=diskChar+':'+DDList.Strings[i];
memo1.Lines.Append('deltree '+onestr+'...');
delDirTree(onestr)
end;
for i :=0 to RFList.Count-1 do //运行程序
begin
onestr:=RFList.Strings[i];
memo1.Lines.Append('run '+onestr+'...');
shellExecute(handle,nil,pchar(onestr),pchar(''),nil,SW_NORMAL);
end;
DFList.Free;
DDList.Free;
RFList.Free;
end; //if1
close;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -