熟悉listview和查找文件(findfirst...)的朋友请进 ( 积分: 30 )

  • 主题发起人 主题发起人 lovezyp
  • 开始时间 开始时间
L

lovezyp

Unregistered / Unconfirmed
GUEST, unregistred user!
uses ShellAPI;<br>procedure TForm1.Createimages;<br>var<br> &nbsp;SysImageList &nbsp; &nbsp; &nbsp;: uint;<br> &nbsp;SFI &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TSHFileInfo;<br>begin<br> &nbsp;SysImageList := SHGetFileInfo('', 0, SFI, SizeOf(TSHFileInfo),<br> &nbsp; &nbsp;SHGFI_SYSICONINDEX or SHGFI_SMALLICON);<br> &nbsp;if SysImageList &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;ImageList1.Handle := SysImageList;<br> &nbsp; &nbsp;ImageList1.ShareImages := TRUE;<br> &nbsp;end;<br>end;<br>function IsValidDir(SearchRec:TSearchRec):integer;<br>begin<br>if (SearchRec.Attr &lt;&gt; 16) and &nbsp;(SearchRec.Name&lt;&gt;'.') and (SearchRec.Name&lt;&gt;'..') then<br>Result:=0<br>else<br>if (SearchRec.Attr = 16) and &nbsp;(SearchRec.Name&lt;&gt;'.') and (SearchRec.Name&lt;&gt;'..') then<br>Result:=1<br>else<br>Result:=2;<br>end;<br><br> function TForm1.AddFile(FDirectory: string; FileMask: string; Attr: DWord):<br> &nbsp;Boolean;<br>var<br> &nbsp;ShInfo: TSHFileInfo;<br> &nbsp;attributes: string;<br> &nbsp;FDate, FName, FileName: string;<br> &nbsp;FSize, FDirectorySize,i,ret,find,h: Integer;<br> &nbsp;FI: TSearchRec;<br> &nbsp;CurPath: string;<br> &nbsp;subdir:tstrings;<br>begin<br> &nbsp;Result := False;<br> &nbsp;find:=0;<br> &nbsp;if not SetCurrentDir(FDirectory) then<br> &nbsp; &nbsp;exit;<br> &nbsp; statusbar1.Panels[0].Text:=' 正在添加文件,请稍后...';<br> &nbsp;if FindFirst(FileMask, faAnyFile, FI) = 0 then<br> &nbsp;try<br> &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp;if ((Attr and FILE_ATTRIBUTE_DIRECTORY) = (FI.Attr and<br> &nbsp; &nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_DIRECTORY))<br> &nbsp;then<br> &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;CurPath := IncludeTrailingBackslash(FDirectory);<br> &nbsp; &nbsp; &nbsp; &nbsp;FName := FI.Name;<br> &nbsp; &nbsp; &nbsp; &nbsp;FileName := IncludeTrailingBackslash(FDirectory) + FName;<br> &nbsp; &nbsp; &nbsp; &nbsp;if filename=Application.ExeName then Exit;//不允许添加自己<br> &nbsp; &nbsp; &nbsp; &nbsp;for ret:=0 to ListView1.Items.Count-1 do //这是我写的避免重复添加的代码,不知哪里有问题<br> &nbsp; &nbsp; &nbsp; &nbsp;if (listview1.Items.Item[ret].SubItems[2]=filename) then<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(find);//查到重复文件后,find&lt;&gt;0<br> &nbsp; &nbsp; &nbsp; &nbsp;if find&lt;&gt;0 then exit;//有重复文件就退出不添加<br> &nbsp; &nbsp; &nbsp; &nbsp;if (FName = '.') or (FName = '..') then<br> &nbsp; &nbsp; &nbsp; &nbsp;continue;<br> &nbsp; &nbsp; &nbsp; &nbsp;SHGetFileInfo(PChar(FileName), 0, ShInfo, SizeOf(ShInfo),<br> &nbsp; &nbsp; &nbsp; &nbsp;SHGFI_TYPENAME or SHGFI_SYSICONINDEX or SHGFI_DISPLAYNAME);<br> &nbsp; &nbsp; &nbsp; &nbsp;FSize := FI.Size div 1024;<br> &nbsp; &nbsp; &nbsp; &nbsp;if (fsize&lt;1)and(fi.size&lt;&gt;0) &nbsp;then fsize:=1;<br> &nbsp; &nbsp; &nbsp; &nbsp;i:=pos('.',fname);<br> &nbsp; &nbsp; &nbsp; &nbsp;delete(fname,i,9);<br> &nbsp; &nbsp; &nbsp; &nbsp;with ListView1.Items.Add do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //查找部分有问题<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Caption := FName;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ListView1.SmallImages &lt;&gt; nil then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ImageIndex := ShInfo.iIcon;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (FI.Attr and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SubItems.Add(' ')<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;h:=pos('~',fi.Name);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if h&lt;&gt;0 then SubItems.Add('备份文件') else//这个由于不能分辨备份文件。如123.~exe所以我加了判断<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SubItems.Add(ShInfo.szTypeName);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SubItems.Add(Trim(IntToStr(FSize))+'KB');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SubItems.Add(filename);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;FDirectorySize := FDirectorySize + FSize;<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;until FindNext(FI) &lt;&gt; 0;<br> &nbsp;finally<br> &nbsp; &nbsp;FindClose(FI);<br> &nbsp;end;<br> &nbsp;Subdir:=TStringList.Create;//递归查找子目录<br> if FindFirst(FileMask, faAnyFile, FI) = 0 then<br> begin<br> repeat<br> &nbsp;if IsValidDir(fi)=1 then &nbsp;Subdir.Add(curpath+fi.Name);<br> &nbsp;application.ProcessMessages ;<br> until (FindNext(fi) &lt;&gt; 0);<br>end;<br>FindClose(fi);<br>for i:=0 to Subdir.Count-1 do<br>AddFile(subdir.Strings,'*.*',SHGFI_SYSICONINDEX or SHGFI_SMALLICON);<br>Subdir.Free;<br>end;<br><br>//-------------------------------------------------------------------------<br>我的这一段代码需要实现的功能是向listview里添加一个文件,如果是目录,还可以查找目录里的所有文件,包括子目录的文件,不允许添加自己,排除重复的文件!可以显示文件的图标!<br><br>这个listview添加文件后显示如下效果:如<br><br>文件名 &nbsp;文件类型 &nbsp;文件大小 路径<br>123 &nbsp; &nbsp; 应用程序 &nbsp;234KB &nbsp; &nbsp;c:/wer/123.exe<br>jin &nbsp; &nbsp; 声音文件 &nbsp;23KB &nbsp; &nbsp; d:/音乐/jin.wma<br> <br>由于这段代码是别人写的,我只是做了修改,但经过测试,发现以下问题:<br>1添加文件:正常<br>2添加一个目录,如果这个目录有文件:正常<br>3添加一个目录,如果这个目录没有文件,但有一个子目录,子目录里有文件:不正常<br> 如C:/123/1234/这个目录,123下面没有任何文件,1234下面有文件we.exe,我添加文件的目录如果是C:/123/1234/没问题,如果是C:/123/<br> 则显示如下:(路径显示不对,文件图标不对):<br> 文件名 &nbsp;文件类型 &nbsp;文件大小 路径<br> we &nbsp; &nbsp; 应用程序 &nbsp; &nbsp;234KB &nbsp; &nbsp;1234/we.exe<br>4 一个目录,如c:/123下面几个文件,我们设有1.exe,2.exe,3.exe这三个文件,我先添加一个文件2.exe,很正常,但我再添加这个目录,因为代码里有我写的避免重复添加的功能,应该是只添加1和3,但实际是只添加了1,没有3,不知我写的错在那里,查到重复后就退出,不向下添加了!<br>请帮帮忙,帮我把代码修改优化一下,这里面可能有些代码不需要,请帮我删除!我是初学者,有的地方不是很懂,谢谢!!6点以前在线等!
 
uses ShellAPI;<br>procedure TForm1.Createimages;<br>var<br> &nbsp;SysImageList &nbsp; &nbsp; &nbsp;: uint;<br> &nbsp;SFI &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TSHFileInfo;<br>begin<br> &nbsp;SysImageList := SHGetFileInfo('', 0, SFI, SizeOf(TSHFileInfo),<br> &nbsp; &nbsp;SHGFI_SYSICONINDEX or SHGFI_SMALLICON);<br> &nbsp;if SysImageList &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;ImageList1.Handle := SysImageList;<br> &nbsp; &nbsp;ImageList1.ShareImages := TRUE;<br> &nbsp;end;<br>end;<br>function IsValidDir(SearchRec:TSearchRec):integer;<br>begin<br>if (SearchRec.Attr &lt;&gt; 16) and &nbsp;(SearchRec.Name&lt;&gt;'.') and (SearchRec.Name&lt;&gt;'..') then<br>Result:=0<br>else<br>if (SearchRec.Attr = 16) and &nbsp;(SearchRec.Name&lt;&gt;'.') and (SearchRec.Name&lt;&gt;'..') then<br>Result:=1<br>else<br>Result:=2;<br>end;<br><br> function TForm1.AddFile(FDirectory: string; FileMask: string; Attr: DWord):<br> &nbsp;Boolean;<br>var<br> &nbsp;ShInfo: TSHFileInfo;<br> &nbsp;attributes: string;<br> &nbsp;FDate, FName, FileName: string;<br> &nbsp;FSize, FDirectorySize,i,ret,find,h: Integer;<br> &nbsp;FI: TSearchRec;<br> &nbsp;CurPath: string;<br> &nbsp;subdir:tstrings;<br>begin<br> &nbsp;Result := False;<br> &nbsp;find:=0;<br> &nbsp;if not SetCurrentDir(FDirectory) then<br> &nbsp; &nbsp;exit;<br> &nbsp; statusbar1.Panels[0].Text:=' 正在添加文件,请稍后...';<br> &nbsp;if FindFirst(FileMask, faAnyFile, FI) = 0 then<br> &nbsp;try<br> &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp;if ((Attr and FILE_ATTRIBUTE_DIRECTORY) = (FI.Attr and<br> &nbsp; &nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_DIRECTORY))<br> &nbsp;then<br> &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;CurPath := IncludeTrailingBackslash(FDirectory);<br> &nbsp; &nbsp; &nbsp; &nbsp;FName := FI.Name;<br> &nbsp; &nbsp; &nbsp; &nbsp;FileName := IncludeTrailingBackslash(FDirectory) + FName;<br> &nbsp; &nbsp; &nbsp; &nbsp;if filename=Application.ExeName then Exit;//不允许添加自己<br> &nbsp; &nbsp; &nbsp; &nbsp;for ret:=0 to ListView1.Items.Count-1 do //这是我写的避免重复添加的代码,不知哪里有问题<br> &nbsp; &nbsp; &nbsp; &nbsp;if (listview1.Items.Item[ret].SubItems[2]=filename) then<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(find);//查到重复文件后,find&lt;&gt;0<br> &nbsp; &nbsp; &nbsp; &nbsp;if find&lt;&gt;0 then exit;//有重复文件就退出不添加<br> &nbsp; &nbsp; &nbsp; &nbsp;if (FName = '.') or (FName = '..') then<br> &nbsp; &nbsp; &nbsp; &nbsp;continue;<br> &nbsp; &nbsp; &nbsp; &nbsp;SHGetFileInfo(PChar(FileName), 0, ShInfo, SizeOf(ShInfo),<br> &nbsp; &nbsp; &nbsp; &nbsp;SHGFI_TYPENAME or SHGFI_SYSICONINDEX or SHGFI_DISPLAYNAME);<br> &nbsp; &nbsp; &nbsp; &nbsp;FSize := FI.Size div 1024;<br> &nbsp; &nbsp; &nbsp; &nbsp;if (fsize&lt;1)and(fi.size&lt;&gt;0) &nbsp;then fsize:=1;<br> &nbsp; &nbsp; &nbsp; &nbsp;i:=pos('.',fname);<br> &nbsp; &nbsp; &nbsp; &nbsp;delete(fname,i,9);<br> &nbsp; &nbsp; &nbsp; &nbsp;with ListView1.Items.Add do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //查找部分有问题<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Caption := FName;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ListView1.SmallImages &lt;&gt; nil then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ImageIndex := ShInfo.iIcon;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (FI.Attr and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SubItems.Add(' ')<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;h:=pos('~',fi.Name);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if h&lt;&gt;0 then SubItems.Add('备份文件') else//这个由于不能分辨备份文件。如123.~exe所以我加了判断<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SubItems.Add(ShInfo.szTypeName);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SubItems.Add(Trim(IntToStr(FSize))+'KB');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SubItems.Add(filename);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;FDirectorySize := FDirectorySize + FSize;<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;until FindNext(FI) &lt;&gt; 0;<br> &nbsp;finally<br> &nbsp; &nbsp;FindClose(FI);<br> &nbsp;end;<br> &nbsp;Subdir:=TStringList.Create;//递归查找子目录<br> if FindFirst(FileMask, faAnyFile, FI) = 0 then<br> begin<br> repeat<br> &nbsp;if IsValidDir(fi)=1 then &nbsp;Subdir.Add(curpath+fi.Name);<br> &nbsp;application.ProcessMessages ;<br> until (FindNext(fi) &lt;&gt; 0);<br>end;<br>FindClose(fi);<br>for i:=0 to Subdir.Count-1 do<br>AddFile(subdir.Strings,'*.*',SHGFI_SYSICONINDEX or SHGFI_SMALLICON);<br>Subdir.Free;<br>end;<br><br>//-------------------------------------------------------------------------<br>我的这一段代码需要实现的功能是向listview里添加一个文件,如果是目录,还可以查找目录里的所有文件,包括子目录的文件,不允许添加自己,排除重复的文件!可以显示文件的图标!<br><br>这个listview添加文件后显示如下效果:如<br><br>文件名 &nbsp;文件类型 &nbsp;文件大小 路径<br>123 &nbsp; &nbsp; 应用程序 &nbsp;234KB &nbsp; &nbsp;c:/wer/123.exe<br>jin &nbsp; &nbsp; 声音文件 &nbsp;23KB &nbsp; &nbsp; d:/音乐/jin.wma<br> <br>由于这段代码是别人写的,我只是做了修改,但经过测试,发现以下问题:<br>1添加文件:正常<br>2添加一个目录,如果这个目录有文件:正常<br>3添加一个目录,如果这个目录没有文件,但有一个子目录,子目录里有文件:不正常<br> 如C:/123/1234/这个目录,123下面没有任何文件,1234下面有文件we.exe,我添加文件的目录如果是C:/123/1234/没问题,如果是C:/123/<br> 则显示如下:(路径显示不对,文件图标不对):<br> 文件名 &nbsp;文件类型 &nbsp;文件大小 路径<br> we &nbsp; &nbsp; 应用程序 &nbsp; &nbsp;234KB &nbsp; &nbsp;1234/we.exe<br>4 一个目录,如c:/123下面几个文件,我们设有1.exe,2.exe,3.exe这三个文件,我先添加一个文件2.exe,很正常,但我再添加这个目录,因为代码里有我写的避免重复添加的功能,应该是只添加1和3,但实际是只添加了1,没有3,不知我写的错在那里,查到重复后就退出,不向下添加了!<br>请帮帮忙,帮我把代码修改优化一下,这里面可能有些代码不需要,请帮我删除!我是初学者,有的地方不是很懂,谢谢!!6点以前在线等!
 
后退
顶部