procedure TForm1.Button1Click(Sender: TObject); var strFileName: string; // holds the name of the file to find FindFileData: TWin32FindData; // holds file information SearchHandle: THandle; // holds the search handle begin {clear any listed files} ListView1.Items.Clear; {if there was no file specified, then specify all files} if Edit2.GetTextLen = 0 then Edit2.Text:= '*.*'; {construct the filename string} strFileName:= DirectoryListBox2.Directory + '\' + Edit2.Text; {set the directory to the specified directory} SetCurrentDirectory(PChar(DirectoryListBox2.Directory)); {begin the search} SearchHandle:=FindFirstFile(PChar(strFileName), FindFileData); {continue searching for all matching files in the current directory} if (SearchHandle <> INVALID_HANDLE_VALUE) then repeat ListView1.Items.Add.Caption:=FindFileData.cFileName; until (FindNextFile(SearchHandle,FindFileData) = FALSE); {all files have been found, so close the search handle} Windows.FindClose(SearchHandle); end;
核心是 SearchHandle:=FindFirstFile(PChar(strFileName), FindFileData); {continue searching for all matching files in the current directory} if (SearchHandle <> INVALID_HANDLE_VALUE) then repeat ListView1.Items.Add.Caption:=FindFileData.cFileName; until (FindNextFile(SearchHandle,FindFileData) = FALSE); {all files have been found, so close the search handle} Windows.FindClose(SearchHandle);
我在我的Blog(http://blog.csdn.net/luckyjan)中已写的很清楚,请看: http://blog.csdn.net/luckyjan/archive/2004/10/10/130657.aspxprocedure TForm1.Button1Click(Sender: TObject); var sr: TSearchRec; begin ListBox1.Items.Clear ; if FindFirst('D:\work\*.*', faAnyFile, sr) = 0 then begin repeat //if pos('.xls',lowercase(sr.Name))>0 then if ((sr.Attr and faDirectory)=0) then ListBox1.Items.Add(sr.Name+ ' '+intToStr(sr.Attr)) ; until FindNext(sr) <> 0; FindClose(sr); end; showMessage(intToStr(ListBox1.Items.count)); end;
procedure TForm1.Button1Click(Sender: TObject); var sr: TSearchRec; begin ListBox1.Items.Clear ; if FindFirst('D:\work\*.*', faAnyFile, sr) = 0 then begin repeat if ((sr.Attr and faDirectory)=0) then ListBox1.Items.Add(sr.Name) ; until FindNext(sr) <> 0; FindClose(sr); end; end;
{--- 查找当前目录及此目录下的子目录中的所有的文件 ---} procedure FindFile(Path: String); var hData: TWin32FindData; hFile: THandle; begin hFile := FindFirstFile(PAnsiChar(Path + '\*.*'), hData); if hFile <> INVALID_HANDLE_VALUE then repeat begin {--- 屏蔽掉'.'和'..'目录 ---} if (hData.cFileName[0] = '.') then Continue; {--- 若找到的是二级文件目录则在此目录下递归查找 ---} if hData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then FindFile(Path + '\' + hData.cFileName) else {--- 若找到文件则向此文件则写出文件名 ---} begin //hData.cFileName就是文件名 Writeln(Path + '\' + hData.cFileName); end; end; until not FindNextFile(hFile, hData) else begin Writeln(Path + ' is not exist'); Exit; end; end;例:C:\Test 是你要查找的目录 FindFile('C:\test');
试一下 typeTFindCallBack=procedure (const filename:string;const info:TSearchRec;var bQuit,bSub:boolean); procedure FindFile(var quit:boolean;const path: String;const filename:string='*.*';proc:TFindCallBack=nil;bSub:boolean=true;const bMsg:boolean=true);varfpath: String;info: TsearchRec;procedure ProcessAFile;beginif (info.Name<>'.') and (info.Name<>'..') and ((info.Attr and faDirectory)<>faDirectory) thenbeginif assigned(proc) thenproc(fpath+info.FindData.cFileName,info,quit,bsub);end;end;没发完,继续
var
strFileName: string; // holds the name of the file to find
FindFileData: TWin32FindData; // holds file information
SearchHandle: THandle; // holds the search handle
begin
{clear any listed files} ListView1.Items.Clear; {if there was no file specified, then specify all files}
if Edit2.GetTextLen = 0 then Edit2.Text:= '*.*'; {construct the filename string}
strFileName:= DirectoryListBox2.Directory + '\' + Edit2.Text; {set the directory to the specified directory}
SetCurrentDirectory(PChar(DirectoryListBox2.Directory)); {begin the search}
SearchHandle:=FindFirstFile(PChar(strFileName), FindFileData); {continue searching for all matching files in the current directory}
if (SearchHandle <> INVALID_HANDLE_VALUE) then
repeat
ListView1.Items.Add.Caption:=FindFileData.cFileName;
until (FindNextFile(SearchHandle,FindFileData) = FALSE); {all files have been found, so close the search handle}
Windows.FindClose(SearchHandle);
end;
SearchHandle:=FindFirstFile(PChar(strFileName), FindFileData); {continue searching for all matching files in the current directory}
if (SearchHandle <> INVALID_HANDLE_VALUE) then
repeat
ListView1.Items.Add.Caption:=FindFileData.cFileName;
until (FindNextFile(SearchHandle,FindFileData) = FALSE); {all files have been found, so close the search handle}
Windows.FindClose(SearchHandle);
我的信箱是[email protected]
QQ;149750032
注意:查找到的结果包含 "." ".." 等目录信息,要判断他并去掉
就可以用FileListBox1.Items.Strings[i]可以得到所有的文件名。
http://blog.csdn.net/luckyjan/archive/2004/10/10/130657.aspxprocedure TForm1.Button1Click(Sender: TObject);
var
sr: TSearchRec;
begin
ListBox1.Items.Clear ;
if FindFirst('D:\work\*.*', faAnyFile, sr) = 0 then
begin
repeat
//if pos('.xls',lowercase(sr.Name))>0 then
if ((sr.Attr and faDirectory)=0) then
ListBox1.Items.Add(sr.Name+ ' '+intToStr(sr.Attr)) ;
until FindNext(sr) <> 0;
FindClose(sr);
end;
showMessage(intToStr(ListBox1.Items.count));
end;
var
sr: TSearchRec;
begin
ListBox1.Items.Clear ;
if FindFirst('D:\work\*.*', faAnyFile, sr) = 0 then
begin
repeat
if ((sr.Attr and faDirectory)=0) then
ListBox1.Items.Add(sr.Name) ;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
procedure FindFile(Path: String);
var
hData: TWin32FindData;
hFile: THandle;
begin
hFile := FindFirstFile(PAnsiChar(Path + '\*.*'), hData);
if hFile <> INVALID_HANDLE_VALUE then
repeat
begin
{--- 屏蔽掉'.'和'..'目录 ---}
if (hData.cFileName[0] = '.') then
Continue;
{--- 若找到的是二级文件目录则在此目录下递归查找 ---}
if hData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then
FindFile(Path + '\' + hData.cFileName)
else
{--- 若找到文件则向此文件则写出文件名 ---}
begin
//hData.cFileName就是文件名
Writeln(Path + '\' + hData.cFileName);
end;
end;
until not FindNextFile(hFile, hData)
else
begin
Writeln(Path + ' is not exist');
Exit;
end;
end;例:C:\Test 是你要查找的目录
FindFile('C:\test');
typeTFindCallBack=procedure (const filename:string;const info:TSearchRec;var bQuit,bSub:boolean); procedure FindFile(var quit:boolean;const path: String;const filename:string='*.*';proc:TFindCallBack=nil;bSub:boolean=true;const bMsg:boolean=true);varfpath: String;info: TsearchRec;procedure ProcessAFile;beginif (info.Name<>'.') and (info.Name<>'..') and ((info.Attr and faDirectory)<>faDirectory) thenbeginif assigned(proc) thenproc(fpath+info.FindData.cFileName,info,quit,bsub);end;end;没发完,继续