主题:看看啊
这段代码有的地方帮我解释下啊.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure DisplayFile(path: string;fileext:string);
procedure GetFileList(FileSpec: string;NamesOnly: Boolean;var FileList: TStringList);
procedure GetSubDirList(DirRoot: string;NamesOnly: Boolean;var SubDirList: TStringList);
function BackSlash(FileSpec: string): string;
function NoBackSlash(FileSpec: string): string;
var
Form1: TForm1;
implementation
{$R *.dfm}
{这个过程显示获取整个目录树中的文件}
procedure DisplayFile(path: string;fileext:string);
var
SubDirList: TStringList;
FileList: TStringList;
i: integer;
begin
SubDirList := TStringList.Create;
try
GetSubDirList(path,False,SubDirList);
{如果这个树含有子目录,递归调用每一个子目录树}
if SubDirList.Count>0 then
begin
for i := 0 to SubDirList.Count-1 do
begin
DisplayFile(SubDirList[i],fileext);
end;
end;
finally
SubDirList.free;
end;
{到这一步所有的子目录树中的对应文件都已被获取,或者根本不存在没有。}
FileList := TStringList.Create;
try
if fileext='' then fileext:='*';
GetFileList(BackSlash(path)+'*.'+fileext,False,FileList);
Form1.ListBox1.Items.AddStrings(FileList);
finally
FileList.Free;
end;
end;
{这个过程把所有匹配文件规格的文件名加入一个StringList。如果NamesOnly是true,那么不包含文件路径}
procedure GetFileList(FileSpec: string;NamesOnly: Boolean;var FileList: TStringList); 这个方法跟下面的不理解var
SR: TSearchRec;
DosError: integer;
begin
FileList.Clear;
DosError := FindFirst(FileSpec, faAnyFile-faDirectory, SR);
while DosError=0 do
begin
if NamesOnly then
FileList.Add(SR.Name)
else
FileList.Add(ExtractFilePath(FileSpec)+SR.Name);
DosError := FindNext(SR);
end;
FindClose(SR);
end;
{这个过程将指定的目录的全部下级目录名加入StringList。如果NamesOnly是true,那么仅仅包括最下级目录名}
procedure GetSubDirList(DirRoot: string;NamesOnly: Boolean;var SubDirList: TStringList); 这个方法不太理解,做什么用的...
var
SR: TSearchRec;
DosError: integer;
Root: string;
begin
SubDirList.Clear;
{在最后加入一个反斜杠(如果不存在)}
Root := BackSlash(DirRoot);
{使用FindFirst/FindNext返回下级目录}
DosError := FindFirst(Root+'*.*', faAnyFile, SR);
//DosError := FindFirst(Root+'*.*', faDirectory + faHidden, SR);
while DosError=0 do
begin
{don't include the directories . and ..}
if (SR.Name<>'.') and (SR.Name<>'..') then
begin
if (SR.Attr and faDirectory)=faDirectory then
begin
if NamesOnly then
SubDirList.Add(SR.Name)
else
SubDirList.Add(Root+SR.Name);
end;
end;
DosError := FindNext(SR);
end;
FindClose(SR);
end;
{添加一个反斜杠(如果它不存在)}
function BackSlash(FileSpec: string): string;
begin
if (FileSpec[length(FileSpec)]<>'\')
then Result := FileSpec+'\'
else Result := FileSpec;
end;
{删除一个反斜杠(如果它存在)}
function NoBackSlash(FileSpec: string): string;
begin
if (FileSpec[length(FileSpec)]='\')
then Result := Copy(FileSpec,1,length(FileSpec)-1)
else Result := FileSpec;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ps:PCHAR;
ls,disklx:DWORD;
SectorsPerCluster,BytesPerSector:DWORD;
NumberOfFreeClusters,TotalNumberOfClusters:DWORD;
i:integer;
str:string;
begin
ListBox1.Items.Clear;
ls:=1024;
i:=1;
str:='';
SectorsPerCluster:=0;
BytesPerSector:=0;
NumberOfFreeClusters:=0;
TotalNumberOfClusters:=0;
Getmem(ps,ls);
try
GetLogicalDriveStrings(ls,ps);
while (ps[i-1]<>chr(0)) or (ps[i]<>chr(0))do
begin
if ps[i]<>chr(0) then
str:=str+ps[i-1]
else
begin
disklx:=GetDriveType(pchar(str));
if disklx=DRIVE_FIXED then
begin
if GetDiskFreeSpace(pchar(str),SectorsPerCluster,BytesPerSector,NumberOfFreeClusters,TotalNumberOfClusters) then
begin
if TotalNumberOfClusters>0 then
begin
//showmessage(str+':'+inttostr(disklx));
str:=BackSlash(str);
DisplayFile(str,'exe');
end;
end;
end;
if (ps[i]=chr(0)) and (ps[i+1]=chr(0)) then break;
str:='';
i:=i+1;
end;
i:=i+1;
end;
finally
freemem(ps);
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure DisplayFile(path: string;fileext:string);
procedure GetFileList(FileSpec: string;NamesOnly: Boolean;var FileList: TStringList);
procedure GetSubDirList(DirRoot: string;NamesOnly: Boolean;var SubDirList: TStringList);
function BackSlash(FileSpec: string): string;
function NoBackSlash(FileSpec: string): string;
var
Form1: TForm1;
implementation
{$R *.dfm}
{这个过程显示获取整个目录树中的文件}
procedure DisplayFile(path: string;fileext:string);
var
SubDirList: TStringList;
FileList: TStringList;
i: integer;
begin
SubDirList := TStringList.Create;
try
GetSubDirList(path,False,SubDirList);
{如果这个树含有子目录,递归调用每一个子目录树}
if SubDirList.Count>0 then
begin
for i := 0 to SubDirList.Count-1 do
begin
DisplayFile(SubDirList[i],fileext);
end;
end;
finally
SubDirList.free;
end;
{到这一步所有的子目录树中的对应文件都已被获取,或者根本不存在没有。}
FileList := TStringList.Create;
try
if fileext='' then fileext:='*';
GetFileList(BackSlash(path)+'*.'+fileext,False,FileList);
Form1.ListBox1.Items.AddStrings(FileList);
finally
FileList.Free;
end;
end;
{这个过程把所有匹配文件规格的文件名加入一个StringList。如果NamesOnly是true,那么不包含文件路径}
procedure GetFileList(FileSpec: string;NamesOnly: Boolean;var FileList: TStringList); 这个方法跟下面的不理解var
SR: TSearchRec;
DosError: integer;
begin
FileList.Clear;
DosError := FindFirst(FileSpec, faAnyFile-faDirectory, SR);
while DosError=0 do
begin
if NamesOnly then
FileList.Add(SR.Name)
else
FileList.Add(ExtractFilePath(FileSpec)+SR.Name);
DosError := FindNext(SR);
end;
FindClose(SR);
end;
{这个过程将指定的目录的全部下级目录名加入StringList。如果NamesOnly是true,那么仅仅包括最下级目录名}
procedure GetSubDirList(DirRoot: string;NamesOnly: Boolean;var SubDirList: TStringList); 这个方法不太理解,做什么用的...
var
SR: TSearchRec;
DosError: integer;
Root: string;
begin
SubDirList.Clear;
{在最后加入一个反斜杠(如果不存在)}
Root := BackSlash(DirRoot);
{使用FindFirst/FindNext返回下级目录}
DosError := FindFirst(Root+'*.*', faAnyFile, SR);
//DosError := FindFirst(Root+'*.*', faDirectory + faHidden, SR);
while DosError=0 do
begin
{don't include the directories . and ..}
if (SR.Name<>'.') and (SR.Name<>'..') then
begin
if (SR.Attr and faDirectory)=faDirectory then
begin
if NamesOnly then
SubDirList.Add(SR.Name)
else
SubDirList.Add(Root+SR.Name);
end;
end;
DosError := FindNext(SR);
end;
FindClose(SR);
end;
{添加一个反斜杠(如果它不存在)}
function BackSlash(FileSpec: string): string;
begin
if (FileSpec[length(FileSpec)]<>'\')
then Result := FileSpec+'\'
else Result := FileSpec;
end;
{删除一个反斜杠(如果它存在)}
function NoBackSlash(FileSpec: string): string;
begin
if (FileSpec[length(FileSpec)]='\')
then Result := Copy(FileSpec,1,length(FileSpec)-1)
else Result := FileSpec;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ps:PCHAR;
ls,disklx:DWORD;
SectorsPerCluster,BytesPerSector:DWORD;
NumberOfFreeClusters,TotalNumberOfClusters:DWORD;
i:integer;
str:string;
begin
ListBox1.Items.Clear;
ls:=1024;
i:=1;
str:='';
SectorsPerCluster:=0;
BytesPerSector:=0;
NumberOfFreeClusters:=0;
TotalNumberOfClusters:=0;
Getmem(ps,ls);
try
GetLogicalDriveStrings(ls,ps);
while (ps[i-1]<>chr(0)) or (ps[i]<>chr(0))do
begin
if ps[i]<>chr(0) then
str:=str+ps[i-1]
else
begin
disklx:=GetDriveType(pchar(str));
if disklx=DRIVE_FIXED then
begin
if GetDiskFreeSpace(pchar(str),SectorsPerCluster,BytesPerSector,NumberOfFreeClusters,TotalNumberOfClusters) then
begin
if TotalNumberOfClusters>0 then
begin
//showmessage(str+':'+inttostr(disklx));
str:=BackSlash(str);
DisplayFile(str,'exe');
end;
end;
end;
if (ps[i]=chr(0)) and (ps[i+1]=chr(0)) then break;
str:='';
i:=i+1;
end;
i:=i+1;
end;
finally
freemem(ps);
end;
end;
end.