这是我写的代码,是把硬盘上的文件进行分类的代码,这是大部分的代码, 你自己用来
修改就可以用了,应该不难吧。
unit PubUnit;
interface
uses
Classes,Messages,ComCtrls,SysUtils,Forms;
const
CM_THREADDONE=WM_USER+$2;
type
TFilterList=array[1..4]of TStringList;
TAllTypeList=array[1..4]of TListView;
function GetLocaPath:string;
implementation
function GetLocaPath:string;
begin
Result:=Extractfiledir(Application.exename);
if length(Result)=3 then
Delete(Result,3,3);
end;
end.
unit FindFileThread;
interface
uses
Windows,Classes,ComCtrls,SysUtils,PubUnit,Grids,StdCtrls;
type
TSanFile= class(TThread)
private
{ Private declarations }
FDir:TStrings;
FPath:string;
FFilters:TFilterList;
//查找文件的后缀列表
//文件添加的字符表
FList:TAllTypeList;
//传导进的列表控件
FFileInfo:TStringGrid;
FileS:array[1..4]of integer;
FMemo:TMemo;
proceduredo
San(const Path:string);
// procedure ScanInf(const FileName:string);
procedure AddInf(const FileName:string;size:integer);
procedure AddInfo(j,i,count,size:integer);
procedure AddLog;
protected
procedure Execute;
override;
public
constructor CreatFindFile(CDir:TStrings;
Filters:TFilterList;
List:TAllTypeList;
FileInfo:TStringGrid;
Memo:TMemo);
destructor Destroy;override;
end;
implementation
uses SanLogUnit;
{ TFindFile }
constructor TSanFile.CreatFindFile(CDir:TStrings;filters:TFilterList;
List:TAllTypeList;FileInfo:TStringGrid;Memo:TMemo);
var
i:integer;
begin
inherited Create(true);
Freeonterminate:=true;
FDir:=CDir;
FFilters:=Filters;
FList:=List;
FFileInfo:=FileInfo;
FMemo:=Memo;
for i:=1 to 4do
FileS:=0;
Resume;
end;
procedure TSanFile.AddLog;
begin
FMemo.Lines.Strings[2]:=' '+FPath;
end;
procedure TSanFile.AddInfo(j,i,count,size:integer);
begin
FFileInfo.Cells[j,i]:=Format('%d 个文件',[count]);
FFileInfo.Cells[j+1,i]:=Format('共计:%d 字节',[size]);
end;
procedure TSanFile.Execute;
const
TABS=#13+#10;
var
i:integer;
bTime,eTime:TDateTime;
begin
bTime:=Now;
Synchronize(AddLog);
for i:=1 to FDir.Countdo
do
San(FDir.Strings[i-1]);
eTime:=Now;
FPath:=' 扫描结束...'+TABS+' 开始时间为:'+DateTimeToStr(bTime)+TABS+
' 结束时间为:'+DateTimeToStr(eTime)+TABS+
' 共计用时为:'+TimeToStr(eTime-bTime);
Synchronize(AddLog);
end;
procedure TSanFile.AddInf(const FileName:string;Size:integer);
var
i,j:integer;
listdata:TListItem;
begin
for j:=1 to 4do
begin
i:=FFilters[j].IndexOf(UpperCase(ExtractFileExt(FileName)));
if i>=0 then
break;
end;
if i<0 then
exit;
listdata:=FList[j].Items.Add;
FileS[j]:=FileS[j]+size;
AddInfo(1,j,FList[j].Items.Count,FileS[j]);
with listdatado
begin
listdata.ImageIndex:=j-1;
Caption:='';
Subitems.Add(ExtractFileName(FileName));//添加文件名
Subitems.Add(FormatFloat('#,###" 字节"',Size));
//添加文件大小
case j of
1:Subitems.Add('音乐文件');
2:Subitems.Add('图片文件');
3:Subitems.Add('视频文件');
4:Subitems.Add('其他文件');
end;
Subitems.Add(FileName);
//添加文件路径
end;
end;
procedure TSanFile.DoSan(const Path:string);
var
FSR:TSearchRec;
begin
if FindFirst(Path+'/*.*',faAnyFile,FSR)=0 then
try
FPath:='当前扫描路径:'+Path+'/';
Synchronize(AddLog);
FindNext(FSR);
while (FindNext(FSR)=0) and (not Terminated)do
begin
//AddLog;
if ((FSR.Attr and faDirectory)<>faDirectory) then
//如果是文件
AddInf(Path+'/'+FSR.Name,FSR.Size)
else
//如果是文件夹
do
San(Path+'/'+FSR.Name);
end;
finally
FindClose(FSR);
end;
end;
destructor TSanFile.Destroy;
begin
PostMessage(ScanLog.Handle,CM_THREADDONE,0,0);
inherited Destroy;
end;
end.