如何返回某个路径下的所有文件名?(100分)

  • 主题发起人 主题发起人 笨人
  • 开始时间 开始时间

笨人

Unregistered / Unconfirmed
GUEST, unregistred user!
如何返回某个路径下的所有文件名?
 
正好我也想问,标记以下。<br><br>我这儿只有查找指定路径下某个文件的代码,不知道对你有没有用。
 
procedure findall(disk,path: String; var fileresult: Tstrings); <br>var <br>fpath: String; <br>fs: TsearchRec; <br>begin <br>fpath:=disk+path+'/*.*'; <br>if findfirst(fpath,faAnyFile,fs)=0 then <br>begin <br>if (fs.Name&lt;&gt;'.')and(fs.Name&lt;&gt;'..') then <br>if (fs.Attr and faDirectory)=faDirectory then <br>findall(disk,path+'/'+fs.Name,fileresult) <br>else <br>fileresult.add(disk+strpas(strupper(pchar(path)))+'/'+strpas( <br>strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')'); <br>while findnext(fs)=0 do <br>begin <br>if (fs.Name&lt;&gt;'.')and(fs.Name&lt;&gt;'..') then <br>if (fs.Attr and faDirectory)=faDirectory then <br>findall(disk,path+'/'+fs.Name,fileresult) <br>else <br>fileresult.add(disk+strpas(strupper(pchar(path)))+'/'+str <br>pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')'); <br>end; <br>end; <br>findclose(fs); <br>end; <br><br>procedure DoSearchFile(Path: string; Files: TStrings = nil);<br>var<br>&nbsp; Info: TSearchRec;<br><br>&nbsp; procedure ProcessAFile(FileName: string);<br>&nbsp; begin<br>&nbsp; &nbsp; if Assigned(PnlPanel) then<br>&nbsp; &nbsp; &nbsp; PnlPanel.Caption := FileName;<br>&nbsp; &nbsp; Label2.Caption := FileName;<br>&nbsp; end;<br><br>&nbsp; function IsDir: Boolean;<br>&nbsp; begin<br>&nbsp; &nbsp; with Info do<br>&nbsp; &nbsp; &nbsp; Result := (Name &lt;&gt; '.') and (Name &lt;&gt; '..') and ((attr and fadirectory) = fadirectory);<br>&nbsp; end;<br><br>&nbsp; function IsFile: Boolean;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := not ((Info.Attr and faDirectory) = faDirectory);<br>&nbsp; end;<br><br>begin<br>&nbsp; Path := IncludeTrailingBackslash(Path);<br>&nbsp; try<br>&nbsp; &nbsp; if FindFirst(Path + '*.*', faAnyFile, Info) = 0 then<br>&nbsp; &nbsp; &nbsp; if IsFile then<br>&nbsp; &nbsp; &nbsp; &nbsp; ProcessAFile(Path + Info.Name)<br>&nbsp; &nbsp; &nbsp; else if IsDir then DoSearchFile(Path + Info.Name);<br>&nbsp; &nbsp; while FindNext(Info) = 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if IsDir then<br>&nbsp; &nbsp; &nbsp; &nbsp; DoSearchFile(Path + Info.Name)<br>&nbsp; &nbsp; &nbsp; else if IsFile then<br>&nbsp; &nbsp; &nbsp; &nbsp; ProcessAFile(Path + Info.Name);<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; if QuitFlag then Break;<br>&nbsp; &nbsp; &nbsp; Sleep(100);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; FindClose(Info);<br>&nbsp; end;<br>end;
 
//在指定目录搜索文件(包括子目录)<br>{<br>参数说明:<br>MainPath:要搜索的文件路径,支持统配符,如:c:/*.*<br>ResultList:保存搜索到的文件列表的TStrings<br>IncludeSubDir:是否包含子目录<br>}<br>procedure hrSearchFile(MainPath: string; ResultList: TStrings; IncludeSubDir: boolean);<br>&nbsp; //判断当前是否是目录<br>&nbsp; function IsValidDir(SearchRec:TSearchRec):Boolean;<br>&nbsp; begin<br>&nbsp; &nbsp;if (SearchRec.Attr = 16) and (SearchRec.Name &lt;&gt; '.') and (SearchRec.Name &lt;&gt; '..') then<br>&nbsp; &nbsp; &nbsp;Result := True<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;Result := False;<br>&nbsp; end;<br><br>&nbsp; //取得指定目录的子目录<br>&nbsp; procedure GetSubDir(MainPath: string; SubDirList: TStrings);<br>&nbsp; var<br>&nbsp; &nbsp; SearchRec: TSearchRec;<br>&nbsp; begin<br>&nbsp; &nbsp; if (FindFirst(ExtractFilePath(MainPath) + '*.*', faDirectory , SearchRec) = 0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if IsValidDir(SearchRec) then<br>&nbsp; &nbsp; &nbsp; &nbsp; SubDirList.Add(SearchRec.Name);<br><br>&nbsp; &nbsp; &nbsp; while (FindNext(SearchRec) = 0) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if IsValidDir(SearchRec) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SubDirList.Add(SearchRec.Name);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; FindClose(SearchRec);<br>&nbsp; end;<br><br>&nbsp; //取得文件<br>&nbsp; procedure GetFiles(MainPath: string; FileList: TStrings; IncludeSubDir: boolean);<br>&nbsp; var<br>&nbsp; &nbsp; SearchRec: TSearchRec;<br>&nbsp; &nbsp; SubDirList: TStringList;<br>&nbsp; &nbsp; I: integer;<br>&nbsp; begin<br>&nbsp; &nbsp; SubDirList := TStringList.Create;<br>&nbsp; &nbsp; //取得当前目录<br>&nbsp; &nbsp; if IncludeSubDir then<br>&nbsp; &nbsp; &nbsp; GetSubDir(MainPath, SubDirList);<br><br>&nbsp; &nbsp; if (FindFirst(MainPath, faAnyFile , SearchRec) = 0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if not IsValidDir(SearchRec) then<br>&nbsp; &nbsp; &nbsp; &nbsp; if SearchRec.Attr &lt;&gt; 16 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileList.Add(ExtractFilePath(MainPath) + SearchRec.Name);<br><br>&nbsp; &nbsp; &nbsp; while (FindNext(SearchRec) = 0) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if not IsValidDir(SearchRec) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if SearchRec.Attr &lt;&gt; 16 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileList.Add(ExtractFilePath(MainPath) + SearchRec.Name);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; FindClose(SearchRec);<br><br>&nbsp; &nbsp; for I := 0 to SubDirList.Count - 1 do<br>&nbsp; &nbsp; &nbsp; GetFiles(ExtractFilePath(MainPath) + SubDirList + '/' + ExtractFileName(MainPath),<br>&nbsp; &nbsp; &nbsp; &nbsp; FileList, IncludeSubDir);<br><br>&nbsp; &nbsp; SubDirList.Free;<br>&nbsp; end;<br>begin<br>&nbsp; ResultList.Clear;<br>&nbsp; GetFiles(MainPath, ResultList, IncludeSubDir);<br>end;
 
说白了就是使用函数<br>FindFirst,FindNext和FindClose三个函数就可以了。对一个文件夹依次查找就可以了。很简单的
 
用 WinApi 也可以<br>FindFirstFile<br>FindNextFile<br>FindClose
 
//查找文件夹下所有文件(包含子文件夹)<br>//path:查找文件路径<br>// &nbsp;fileresult:得到文件列表<br>procedure findall(path: String; var fileresult: Tstrings);<br>var <br>&nbsp; fpath: String;<br>&nbsp; fs: TsearchRec;<br>begin <br>&nbsp;fpath:=path+'/*.*';<br>&nbsp; if findfirst(fpath,faAnyFile,fs)=0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (fs.Name&lt;&gt;'.')and(fs.Name&lt;&gt;'..') then<br>&nbsp; &nbsp; &nbsp; &nbsp; if (fs.Attr and faDirectory)=faDirectory then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findall(path+'/'+fs.Name,fileresult)<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; fileresult.add(strpas(strupper(pchar(path)))+'/'+strpas(<br>&nbsp; &nbsp; &nbsp; &nbsp; strupper(pchar(fs.Name))));<br>&nbsp; &nbsp; &nbsp; &nbsp; while findnext(fs)=0 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fs.Name&lt;&gt;'.')and(fs.Name&lt;&gt;'..') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fs.Attr and faDirectory)=faDirectory then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findall(path+'/'+fs.Name,fileresult)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileresult.add(strpas(strupper(pchar(path)))+'/'+strpas(strupper(pchar(fs.Name))));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; findclose(fs);<br>end;<br><br>//get files in directory,不包括子文件夹<br>procedure ListFileDir(Path: string; FileList: TStrings);<br>var<br>&nbsp; SR: TSearchRec;<br>&nbsp; path0:string;<br>begin<br>&nbsp; &nbsp;path0:=backslash(path);<br>&nbsp; if FindFirst(Path0 + '*.*', faAnyFile, SR) = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; if (SR.Attr &lt;&gt; faDirectory) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; FileList.Add(SR.Name);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; until FindNext(SR) &lt;&gt; 0;<br>&nbsp; &nbsp; FindClose(SR);<br>&nbsp; end;<br>end;<br><br>
 
后退
顶部