找出指定目录下的所有以.abc结尾的文件
findSch(path:sting;Fname:string):Tstrings;不知明白否
在线等~~
findSch(path:sting;Fname:string):Tstrings;不知明白否
在线等~~
解决方案 »
- 如果对多个select语句的结果进行四则运算?大家帮忙看看~~谢谢~~
- 如何判断一个Blob字段有内容?
- delphi7.0用websnap开发websnap应用程序?
- 把菜单放在TOOLBAR里再把TOOLBAR放在ControlBar里会出现菜单快捷键无效,请问如何解决?
- 请高手关注数据库应用程序的难题攻克? -------当场测试通过就给分!!
- 有专用的算法基础书吗
- Delphi怎样在Access数据库中加入图片?
- 请问各位,DELPHI中的流是什么东西呀
- 这个问题有点麻烦,如何快速得到一个图片的分辨率和颜色位数的信息?(不完全load)
- Tquery 的在 Tdatabase 設有事務處理時,他能起作用嗎!
- 请问Delphi中的转义字符??
- 找资料
FINDFIRST;
var
Found: TSearchRec;
i: integer;
Dirs: TStrings;
Finished: integer;
StopSearch: Boolean;
begin
StopSearch := False;
Dirs := TStringList.Create;
Finished := FindFirst(Dir + '*.*', 63, Found);
while (Finished = 0) and not (StopSearch) do
begin
if (Found.Name <> '.') then
begin
if (Found.Attr and faDirectory) = faDirectory then
Dirs.Add(Dir + Found.Name)
else
if Pos(UpperCase(Ext), UpperCase(Found.Name)) > 0 then
Files.Add(Dir + Found.Name);
end;
Finished := FindNext(Found);
end;
FindClose(Found);
if not StopSearch then
for i := 0 to Dirs.Count - 1 do
SearchFileEx(Dirs[i], Ext, Files);
Dirs.Free;
end;Files中所存既是
procedure Tbackfrm.refreshlistview;
var
sr: TSearchRec;
filename: TStrings;
dirName: string;
i:integer;
begin
dirName := Extractfilepath(application.exeName)+'back\';// 应用程序back文件夹下
filename:= TStringList.Create;
if FindFirst(dirName + '*.bak',faAnyFile,sr) = 0 then//所有以bak为扩展名,你可以指定一下。
begin
repeat
if (sr.Attr and faAnyFile) = sr.Attr then
filename.Add(sr.Name); until FindNext(sr) <> 0;
FindClose(sr);
for i:=0 to filename.Count-1 do
begin
listview1.Items.Add;
listview1.Items.Item[i].Caption:=filename[i]
end;
filename.Free;
end;
end;
var FileStr: string);
{ 此过程用于从一个文件中扫描字符串}
var
Marker: string[1];
FoundOnce: Boolean;
FindPos: integer;
begin
FindPos := Pos(SearchStr, FileStr);
FoundOnce := False;
while (FindPos <> 0) and not Terminated do
begin
if not FoundOnce then
begin
if FileNames then
Marker := ''
else
Marker := ':';
{ 添加文件到列表框}
AddStr := Format('File %s%s', [FName, Marker]);
Synchronize(AddToList);
FoundOnce := True;
end;
if FileNames then Exit; AddStr := GetCurLine(FileStr, FindPos);
Synchronize(AddToList);
FileStr := Copy(FileStr, FindPos + Length(SearchStr),
Length(FileStr));
FindPos := Pos(SearchStr, FileStr);
end;
end;procedure TSearchThread.SearchFile(const FName: string);
{ 此过程用于搜索文件名称 }
var
DataFile: THandle;
FileSize: Integer;
SearchString: string;
begin
FSearchFile := FName;
Synchronize(SetSearchFile);
try
DataFile := FileOpen(FName, fmOpenRead or fmShareDenyWrite);
if DataFile = 0 then raise Exception.Create('');
try
FileSize := GetFileSize(DataFile, nil);
SetLength(SearchString, FileSize);
FileRead(DataFile, Pointer(SearchString)^, FileSize);
finally
CloseHandle(DataFile);
end;
if not CaseSens then SearchString := UpperCase(SearchString);
ScanForStr(FName, SearchString);
except
on Exception do
begin
AddStr := Format('Error reading file: %s', [FName]);
Synchronize(AddToList);
end;
end;
end;procedure TSearchThread.FindAllFiles(const Path: string);
{ 查找所有匹配的文件}
var
SR: TSearchRec;
begin
{ 查找第一个匹配的文件}
if FindFirst(Path + FileSpec, faArchive, SR) = 0 then
try
repeat
SearchFile(Path + SR.Name);
until (FindNext(SR) <> 0) or Terminated; // 寻找下一个文件
finally
SysUtils.FindClose(SR); // 最后清除查找记录
end;
end;procedure TSearchThread.DoSearch(const Path: string);
{该过程用于在子目录树中递归查找}
var
SR: TSearchRec;
begin
{ 查找目录 }
if FindFirst(Path + '*.*', faDirectory, SR) = 0 then
try
repeat
{ 如果是一个目录... }
if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') and
not Terminated then
begin
FindAllFiles(Path + SR.Name + '\'); // 在目录中查找
DoSearch(Path + SR.Name + '\'); // 递归查找
end;
until (FindNext(SR) <> 0) or Terminated; // 寻找下一个目录
finally
SysUtils.FindClose(SR); // 最后清除查找记录
end;
end;
showmessage('Found a exe file')
程序无法用,编译错误
能不能转成函数啊
type
TForm1 = class(TForm)
Button1: TButton;
CheckBox1: TCheckBox;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
end;var
Form1: TForm1;implementation
{$R *.dfm}type
TCallBackFind = function(const Path, FileName: string; Param: Pointer): Boolean; stdcall;
PFindFile = ^TFindFile;
TFindFile = record
Path: string;
Param: Pointer;
Callback: TCallBackFind;
end;function FindFileThread(Find: PFindFile): DWORD; stdcall; function DoCallback(const Path, FileName: string): Boolean;
begin
Result := False;
if Assigned(Find^.Callback) then
Result := Find^.Callback(Path, FileName, Find^.Param);
end; function SearchPath(const Path: string): Boolean;
var
Rec: TSearchRec;
begin
Result := True;
if FindFirst(Path + '\*.*', faAnyFile, Rec) = 0 then
try
repeat
if (Rec.Name <> '.') and (Rec.Name <> '..') then
begin
if (Rec.Attr and faDirectory) = faDirectory then
Result := SearchPath(Path + '\' + Rec.Name)
else
Result := DoCallback(Path, Rec.Name);
if not Result then break;
end;
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
end;begin
Result := 0;
if not Assigned(Find) then Exit;
try
SearchPath(Find^.Path);
finally
FreeMem(Find);
end;
end;procedure FindAllFile(const Path: string; Callback: TCallbackFind; Param: Pointer);
var
Find: PFindFile;
ThreadID: Cardinal;
begin
New(Find);
Find^.Path := Path;
Find^.Param := Param;
Find^.Callback := Callback;
CloseHandle(CreateThread(nil, 0, @FindFileThread, Find, 0, ThreadID));
end;function FindCall(const Path, FileName: string; Form: TForm1): Boolean; stdcall;
begin
Result := Form.CheckBox1.Checked;
if Result then
Form.Memo1.Lines.Add(Format('Path: %s, FileName: %s', [Path, FileName]));
//Application.ProcessMessages;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
CheckBox1.Checked := True;
FindAllFile('c:', @FindCall, Self);
end;
看不太明白
能不能写成函数啊,让我直接调用就可以了
邮箱 [email protected]
var
Ext: string;
begin
Result := True;
Ext := ExtractFileExt(FileName);
if SameText(Ext, '.abc') then
begin
List.Add(Path + FileName);
//List.Add(Format('Path: %s, FileName: %s', [Path, FileName]));
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
// 从C盘找文件(包括子目录),找到一个文件则调用FindCall函数(回调),
// Memo1.Lines为参数,对应FindCall的List参数。
FindAllFile('c:', @FindCall, Memo1.Lines);
end;