我写了一个文件查找的过程,在处理C:\时,得到结果正常,可是在处理C:\WINDOWS\时,得到的结果不正确,目录和文件混淆,并且数量也不对(50分)

  • 主题发起人 主题发起人 wp231957
  • 开始时间 开始时间
W

wp231957

Unregistered / Unconfirmed
GUEST, unregistred user!
//查找文件<br>//RETD 返回查找目录名<br>//RETF 返回查找文件名<br>procedure find_file(rootpath:string;var retd:tstringlist;var retf:tstringlist);<br>var<br> &nbsp;wfd:WIN32_FIND_DATA;<br> &nbsp;hfile:dword;<br>begin<br> &nbsp;hfile:=findfirstfile(pchar(rootpath),wfd);<br> &nbsp;if hfile&lt;&gt;INVALID_HANDLE_VALUE then begin<br> &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp;if wfd.dwFileAttributes =FILE_ATTRIBUTE_DIRECTORY then<br> &nbsp; &nbsp; &nbsp; &nbsp;retd.Add(wfd.cFileName)<br> &nbsp; &nbsp; &nbsp;else retf.Add(wfd.cFileName);<br> &nbsp; &nbsp;until findnextfile(hfile,wfd)=false;<br> &nbsp;end;<br>end;
 
调用过程如下<br>find_file('c:/windows/*.*',tsd,tsf);
 
我的要求很简单,就是获取一级子目录列表和文件列表,对于子目录里的东东不用考虑
 
不知道改成find_file('c:/windows',tsd,tsf);行不行<br>不过最后最好用FindClose(hfile)把Handle给关掉。
 
楼上的似乎不行呢
 
procedure SearchLocalFile(const Dir, FilesFilter: string;<br> &nbsp;SearchSubDir: Boolean);<br>var<br> &nbsp;SearchRec: TSearchRec;<br>begin<br> &nbsp;if FindFirst(Dir + FilesFilter, faAnyFile, SearchRec) = 0 then<br> &nbsp;try<br> &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp;if (SearchRec.Name &lt;&gt; '.') and (SearchRec.Name &lt;&gt; '..') then &nbsp;//排除自身和父目录<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if (SearchRec.Attr and faDirectory) &gt; 0 then //如果是目录的话<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if SearchSubDir then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SearchLocalFile(IncludeTrailingPathDelimiter(Dir + SearchRec.Name),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FilesFilter, SearchSubDir);<br> &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else //不是目录,是文件<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if AcceptFileToAdd(SearchRec.Name, ['.mp3']) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AddLocalFileToListView(Dir + SearchRec.Name, lvOpenFiles);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;until FindNext(SearchRec) &lt;&gt; 0;<br> &nbsp;finally<br> &nbsp; &nbsp;FindClose(SearchRec);<br> &nbsp;end;<br>end;<br>这是正确的代码,楼主自己看错在哪里。<br>SearchLocalFile('C:/Windows', '*.*', True);
 
procedure find_file2(rootpath:string;var retd:tstringlist;var retf:tstringlist);<br>var<br> &nbsp;sr: TSearchRec;<br> &nbsp;FileAttrs :integer;<br>begin<br> &nbsp;FileAttrs :=faSymLink+ faHidden+faSysFile+faDirectory+faAnyFile+faArchive+faReadOnly+faVolumeID ;<br> &nbsp;if FindFirst(rootpath, FileAttrs, sr) = 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp;if (sr.Attr and faDirectory) &gt;0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;retd.Add(sr.Name )<br> &nbsp; &nbsp; &nbsp;else retf.Add(sr.Name );<br> &nbsp; &nbsp;until FindNext(sr) &lt;&gt; 0;<br> &nbsp; &nbsp;FindClose(sr);<br> &nbsp;end;<br>end;<br>我用如上代码 获取的目录数量是 正确的<br>获取文件的数量在C:/WINDOWS/SYSTEM32下是正确的,和在CMD下用DIR /A 所列的目录一样多<br>而在获取C:/WINDOWS下的目录数是正确的,而获取文件数则比DIR /A少了3个文件,不知道差在哪里??????????
 
少了哪三个文件呢,和这些文件的属性有关系吧。
 
那么多的文件,我一时之间还不晓得缺了哪3个 文件
 
// if wfd.dwFileAttributes =FILE_ATTRIBUTE_DIRECTORY then<br>改为:<br>if wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY &gt; 0 then
 
后退
顶部