type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
procedure GetDiskInfo;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//------------------------------------------------------------------------------
//查找目录下所有文件
function ListAllFiles(const FilePath: string; var FileList:TStringList): Integer;
var
SearchRec: TSearchRec;
ReValue: Integer;
CurrentFilePath: string;
function fcCheckDirectoryFormat(const FilePath:string):string;
begin
Result:=FilePath;
if FilePath[Length(FilePath)]<>'\' then Result:=FilePath+'\';
end;
begin
CurrentFilePath := fcCheckDirectoryFormat(FilePath);
ReValue := FindFirst(CurrentFilePath + '*.*', faAnyFile, SearchRec);
while ReValue = 0 do
begin
if ((SearchRec.Attr and faDirectory) = faDirectory) then //文件夹
begin
if not ((SearchRec.Name = '.') or (SearchRec.Name = '..')) then
begin //递归查找下一个目录
ListAllFiles(CurrentFilePath + SearchRec.Name, FileList);
end;
end
else //普通文件夹
begin
if ExtractFileExt(UpperCase(SearchRec.Name))='.HTM' then
begin
FileList.Add(CurrentFilePath + SearchRec.Name);
end;
end;
ReValue := FindNext(SearchRec);
end;
result := FileList.Count;
end;
//------------------------------------------------------------------------------
//得到有效的硬盘分区
procedure TForm1.GetDiskInfo;
var
i:Integer;
vstring:string;
DTYPE:Integer;
lst:TStringList;
begin
lst:=TStringList.Create;
try
Application.ProcessMessages;
For i:=65 to 90 do
begin
vString:=Chr(i)+':\';
DTYPE:=GetDriveType(PChar(vString));
if DType=DRIVE_FIXED then ListAllFiles(vString,lst);
end;
ListBox1.Items.Assign(lst);
finally
lst.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GetDiskInfo;
end;
var AvailableDrives :dword;
AvailableDrives := GetLogicalDrives;
for i:= 0 to 25 do
if GetBit(AvailableDrives,i+1)=1 then//如果驱动器存在
begin
DrivePath:=Char(Ord('A')+i)+':\';
if GetDriveType(pchar(DrivePath)) = DRIVE_FIXED then
begin
//硬盘
end;
end;
2、扫描文件
function ListAllFiles(const MetterPath, FilePath: string; var FileList:
TStringList): Integer;
var
SearchRec: TSearchRec;
ReValue: Integer;
CurrentMetterPath: string;
CurrentFilePath: string;
FileExt: string;
FileCaption: string;
begin
CurrentFilePath := fcCheckDirectoryFormat(FilePath);
CurrentMetterPath := fcCheckDirectoryFormat(MetterPath);
ReValue := FindFirst(CurrentFilePath + '*.*', faAnyFile, SearchRec);
while ReValue = 0 do
begin
if ((SearchRec.Attr and faDirectory) = faDirectory) then
begin
if not ((SearchRec.Name = '.') or (SearchRec.Name = '..')) then
begin
ListAllFiles(CurrentMetterPath + SearchRec.Name,
CurrentFilePath + SearchRec.Name, FileList);
end;
end
else //普通文件夹
begin
if Pos('.htm', UpperCase(SearchRec.Name)) > 0 then
begin
FileCaption := fcGetFileCaption(SearchRec.Name, False);
FileList.Add(CurrentMetterPath + '/' +
FileCaption + '/' + CurrentFilePath + SearchRec.Name);
end;
end;
ReValue := FindNext(SearchRec);
end;
result := FileList.Count;
end;
1、扫描硬盘分区
var
i:Integer;
vstring:string;
DTYPE:Integer;
begin
For i:=65 to 90 do
begin
vString:=Chr(i)+':\'
DTYPE:=GetDriveType(PChar(vString));
if DType=DRIVE_FIXED then
showmessage('是有效的硬盘分区')
end;
end;
《待续》