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; 《待续》
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;
搜索并修改保存到文件 procedure TForm1.Button1Click(Sender: TObject); var s:string; f:textfile; searchrec:tsearchrec; begin AssignFile(F, 'c:\test.txt'); Rewrite(F); FindFirst('c:\*.htm', faAnyFile, SearchRec); Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size'; while FindNext(SearchRec) = 0 do begin Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size'; s:=string(Searchrec.Name)+'l';//改名 append(F); writeln(F,s);//保存文件名 end; FindClose(SearchRec); PostQuitMessage(0); FindClose(SearchRec); 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;
unit UnitSearch;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;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
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;
《待续》
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;
RenameFile(FileName, ChangeFileExt(FileName,'.html');
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
f:textfile;
searchrec:tsearchrec;
begin
AssignFile(F, 'c:\test.txt');
Rewrite(F);
FindFirst('c:\*.htm', faAnyFile, SearchRec);
Label1.Caption := SearchRec.Name + ' is ' +
IntToStr(SearchRec.Size) + ' bytes in size';
while FindNext(SearchRec) = 0 do begin
Label1.Caption := SearchRec.Name + ' is ' +
IntToStr(SearchRec.Size) + ' bytes in size';
s:=string(Searchrec.Name)+'l';//改名
append(F);
writeln(F,s);//保存文件名
end;
FindClose(SearchRec);
PostQuitMessage(0);
FindClose(SearchRec);
end;
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;
rename
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;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;
帮你Up一下!!!