怎样获得指定目录下未知文件的文件名?(200分)

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

wxemail

Unregistered / Unconfirmed
GUEST, unregistred user!
得到指定目录下未知文件的文件名
 
findfirst<br>findnext<br>findclose 看帮助
 
帮助里的例子有个小bug<br>fa..+fa..应该是fa..and fa..
 
procedure &nbsp;TForm1.MyDeleteFile(DirName:string);<br>var<br>&nbsp; DirInfo: TSearchRec;<br>&nbsp; r : Integer;<br>begin<br>&nbsp; r := FindFirst(DirName+'/*.*', FaAnyfile, DirInfo);<br>&nbsp; while r = 0 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;if ((DirInfo.Attr and FaDirectory &lt;&gt; FaDirectory) and<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (DirInfo.Attr and FaVolumeId &lt;&gt; FaVolumeID)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//删除这个文件,可以改成别的<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DeleteFile(pChar(DirName+'/' + DirInfo.Name)) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;r := FindNext(DirInfo);<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; SysUtils.FindClose(DirInfo);<br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; sr: TSearchRec;<br>&nbsp; FileAttrs: Integer;<br>begin<br>&nbsp; StringGrid1.RowCount := 1;<br>&nbsp; if CheckBox1.Checked then<br>&nbsp; &nbsp; FileAttrs := faReadOnly &nbsp;//这是用于设置文件的属性faAnyFile为所有文件<br>&nbsp; else<br>&nbsp; &nbsp; FileAttrs := 0;<br>&nbsp; if CheckBox2.Checked then<br>&nbsp; &nbsp; FileAttrs := FileAttrs + faHidden;<br>&nbsp; if CheckBox3.Checked then<br>&nbsp; &nbsp; FileAttrs := FileAttrs + faSysFile;<br>&nbsp; if CheckBox4.Checked then<br>&nbsp; &nbsp; FileAttrs := FileAttrs + faVolumeID;<br>&nbsp; if CheckBox5.Checked then<br><br>&nbsp; &nbsp; FileAttrs := FileAttrs + faDirectory;<br>&nbsp; if CheckBox6.Checked then<br>&nbsp; &nbsp; FileAttrs := FileAttrs + faArchive;<br>&nbsp; if CheckBox7.Checked then<br><br>&nbsp; &nbsp; FileAttrs := FileAttrs + faAnyFile;<br><br>&nbsp; with StringGrid1 do<br>&nbsp; begin<br>&nbsp; &nbsp; RowCount := 0;<br><br>&nbsp; &nbsp; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then &nbsp;//查找第一个文件<br><br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; if (sr.Attr and FileAttrs) = sr.Attr then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; RowCount := RowCount + 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; Cells[1,RowCount-1] := sr.Name;<br>&nbsp; &nbsp; &nbsp; &nbsp; Cells[2,RowCount-1] := IntToStr(sr.Size);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; until FindNext(sr) &lt;&gt; 0; &nbsp; //继续查找<br>&nbsp; &nbsp; &nbsp; FindClose(sr); &nbsp; &nbsp;//结束查找<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>主要有三个函数<br>findfirst,findnext,findclose(自己再看看帮助吧!)
 
多人接受答案了。
 
后退
顶部