高分求购以下代码,各位大虾来帮忙啊。关于文件操作的。(200分)

  • 主题发起人 ch_yahuu2
  • 开始时间
C

ch_yahuu2

Unregistered / Unconfirmed
GUEST, unregistred user!
如果在指定目录下查找最近被修改过的一个扩展名为aaa的文件。并得到文件名。<br>比如c:/abc/中有10个文件,其中扩展名为aaa的5个。要求在这5个文件中找到最近一个被<br>修改的文件。请大虾们给出完整的代码。谢谢。<br>
 
留下信箱
 
ch_yahuu@163.com<br>先谢谢了。
 
怎么没人说话了啊?高手啊。我要高手啊。楼上的兄弟怎么不回话了啊。邮件也还没有给我发啊<br><br>功能解决了。马上就给分啊。。。。<br><br>给大家两个参考的东西。可是我不知道用什么方法可以读出TStringList中的数据:<br>第一个:<br><br>//具体参数:mainpath,路径;fileextern:扩展名;filename:获得的文件列表;<br>procedure SearchFile(mainpath,FileExtern:string;var FileName:TStringList);<br><br>procedure SearchFile(Mainpath,FileExtern:string;var FileName:TStringList);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; Status: Integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; SearchRec: TSearchRec;<br>&nbsp; &nbsp; &nbsp; &nbsp; function SlashSep(const Path, S: string): string;<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if AnsiLastChar(Path)^ &lt;&gt; '/' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := Path + '/' + S<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := Path + S;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Status:= FindFirst(SlashSep(Mainpath,FileExtern),faAnyFile,SearchRec);<br>&nbsp; &nbsp; &nbsp; &nbsp; while Status=0 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileName.Add(SearchRec.Name);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Status:=FindNext(SearchRec);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; FindClose(SearchRec);<br>end;<br><br>第二个:<br><br><br>size := FileSize(f); &nbsp;//文件长度<br>sText := ExtractFileExt(filename) ; &nbsp;//文件扩展名<br>Stext:=FormatDateTime('yyyy-mm-dd hh:nn:ss',FGetFileTime(filename,0)); &nbsp;//文件创建时间<br>Stext:=FormatDateTime('yyyy-mm-dd hh:nn:ss',FGetFileTime(filename,1)); &nbsp;//文件修改时间<br>Stext:=FormatDateTime('yyyy-mm-dd',FGetFileTime(filename,2)); &nbsp;//文件最后访问日期<br><br>然后就是时间相比较了。请如如何比较。什么方法简单点,速度快点。<br><br>希望有高手通过上面的参考给我一个如主题的代码。先谢谢了。
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, FileCtrl,Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Buttons;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; BitBtn1: TBitBtn;<br>&nbsp; &nbsp; LabelTotal: TLabel;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure BitBtn1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br><br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; Total:integer;<br>implementation<br><br>{$R *.DFM}<br><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><br>end;<br><br>Procedure SearchFile(mainpath:string;filename:string;foundresult:TStrings;var Num:integer);<br>var<br>i:integer;<br>Subdir1:TStrings;<br>searchRec:TsearchRec;<br>begin<br>if Trim(filename)&lt;&gt;'' then<br>begin<br>if (FindFirst(mainpath+Filename, faAnyFile, SearchRec)=0) then<br>&nbsp;Begin<br>&nbsp; repeat<br>&nbsp; &nbsp; if IsValidDir(SearchRec)=0 then<br>&nbsp; &nbsp; &nbsp; begin Inc(Num); foundresult.Add(mainpath+SearchRec.Name) end;<br>&nbsp; &nbsp; application.ProcessMessages ;<br>&nbsp; until (FindNext(SearchRec) &lt;&gt; 0);<br>&nbsp;end;<br>FindClose(SearchRec);<br>//这是递归部分,查找各子目录。<br>Subdir1:=TStringList.Create;//字符串列表必须动态生成<br>if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then<br>begin<br>&nbsp;repeat<br>&nbsp; if IsValidDir(SearchRec)=1 then &nbsp;Subdir1.Add(SearchRec.Name);<br>&nbsp; application.ProcessMessages ;<br>&nbsp;until (FindNext(SearchRec) &lt;&gt; 0);<br>end;<br>FindClose(SearchRec);<br>for i:=0 to Subdir1.Count-1 do<br>&nbsp;Searchfile(mainpath+Subdir1.Strings+'/',Filename,foundresult,Num);<br>//资源释放并返回结果。<br>Subdir1.Free;<br>end;<br><br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp;i,f:Integer;<br>&nbsp;rf:String;<br>&nbsp;tmpDate:TDateTime;<br>Begin<br>memo1.lines.Clear ;<br>Total:=0;<br>SearchFile(Edit2.Text,Edit1.Text,memo1.lines, Total);<br>LabelTotal.Caption :=format('查找文件总数为:%d',[Total]);<br><br>for i := 0 to Memo1.Lines.Count-1 do<br>begin<br>&nbsp; f := FileOpen(Memo1.Lines, fmOpenRead);<br>&nbsp; if i=0 then begin<br>&nbsp; &nbsp; &nbsp;tmpDate:=fileDateToDateTime(FileGetDate(f));<br>&nbsp; &nbsp; &nbsp;rf:=Memo1.Lines[0];<br>&nbsp; end;<br>&nbsp; //比较时间<br>&nbsp; if fileDateToDateTime(fileGetDate(f))&gt;tmpDate then begin<br>&nbsp; &nbsp; &nbsp;tmpDate:=fileDateToDateTime(fileGetDate(f));<br>&nbsp; &nbsp; &nbsp;rf:=Memo1.Lines;<br>&nbsp; end;<br>&nbsp; FileClose(f);<br>end;<br>&nbsp;//显示符合要求的文件。<br>&nbsp;showmessage(rf);<br>&nbsp;label3.Caption:=rf;<br>end;<br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>var dir:string;<br>begin<br>if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt],0) then<br>&nbsp;Begin<br>&nbsp; &nbsp;if length(dir) &gt; 3 then dir:=dir+'/';<br>&nbsp; &nbsp;Edit2.Text := dir;<br>&nbsp;end;<br>end;<br><br>end.
 
楼上的大哥。那个SelectDirectory是什么意思。delphi不认他啊。<br>我把bitbton1设为注解后可以编译通过。但我试了还是不行。无任何显示。<br>你能给我改成直接查找c:/abc &nbsp;目录下的所有扩展名为exe的且修改日期为最近的文件。<br>然后把这个文件名在edit1中显示出来吗?<br><br>如成功的话。马上给分。<br>因为我已知道我要查找的文件所在的目录了。
 
type<br>&nbsp; TFileTimeType = (fttCreation, fttLastAccess, fttLastWrite);<br><br>function GetFileDateTime(const FileName: string; FileTimeType: TFileTimeType): string;<br>var<br>&nbsp; Handle: THandle;<br>&nbsp; FindData: TWin32FindData;<br>&nbsp; LocalFileTime: TFileTime;<br>&nbsp; DosDateTime: Integer;<br>begin<br>&nbsp; Result := '';<br>&nbsp; Handle := FindFirstFile(PChar(FileName), FindData);<br>&nbsp; if Handle &lt;&gt; INVALID_HANDLE_VALUE then<br>&nbsp; begin<br>&nbsp; &nbsp; Windows.FindClose(Handle);<br>&nbsp; &nbsp; if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; case FileTimeType of<br>&nbsp; &nbsp; &nbsp; &nbsp; fttCreation: FileTimeToLocalFileTime(FindData.ftCreationTime, LocalFileTime);<br>&nbsp; &nbsp; &nbsp; &nbsp; fttLastAccess: FileTimeToLocalFileTime(FindData.ftLastAccessTime, LocalFileTime);<br>&nbsp; &nbsp; &nbsp; &nbsp; fttLastWrite: FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; if FileTimeToDosDateTime(LocalFileTime, LongRec(DosDateTime).Hi, LongRec(DosDateTime).Lo) then<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := FormatDateTime('yyyy-mm-dd hh:mm:ss', FileDateToDateTime(DosDateTime));<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; temp,i,t: Longint;<br>&nbsp; filename: string;<br>begin<br>&nbsp; listbox1.Items.Clear;<br>&nbsp; Total := 0;<br>&nbsp; temp := 0;<br>&nbsp; t:=0;<br>&nbsp; SearchFile(Edit2.Text, Edit1.Text, listbox1.Items, Total);<br>&nbsp; LabelTotal.Caption := format('查找文件总数为:%d', [Total]);<br>&nbsp; for i := 0 to listbox1.Items.count - 1 do begin<br>&nbsp; &nbsp; filename := listbox1.Items;<br>&nbsp; &nbsp; if GetFileTime(FileName) &gt; temp then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;temp := GetFileTime(FileName);<br>&nbsp; &nbsp; &nbsp; &nbsp;t:=i;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; //找到最近一个被修改的文件<br>&nbsp; Label5.Caption :='路径:'+ExtractFilePath(listbox1.Items[t]);<br>&nbsp; Label3.Caption :='文件名:'+ExtractFileName(listbox1.Items[t]);<br>&nbsp; Label4.Caption :='修改时间:'+ GetFileDateTime(listbox1.Items[t],fttLastWrite);<br>end;<br><br>
 
procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; sr: TSearchRec;<br>&nbsp; FileAttrs: Integer;<br>&nbsp; t,nt:TDateTime;<br>&nbsp; f:String;<br>begin<br>&nbsp; f:='';<br>&nbsp; t:=0;<br>&nbsp; FileAttrs :=faAnyFile;<br>&nbsp; if FindFirst('c:/abc/*.aaa', FileAttrs, sr) = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (sr.Attr and FileAttrs) = sr.Attr then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; f:=sr.Name;<br>&nbsp; &nbsp; &nbsp; &nbsp; t:=FileDateToDateTime(sr.time);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; while FindNext(sr) = 0 do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if (sr.Attr and FileAttrs) = sr.Attr then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nt:=FileDateToDateTime(sr.time);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (t&lt;nt) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t:=nt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f:=sr.Name;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; FindClose(sr);<br>&nbsp; end;<br>&nbsp; showmessage(f);<br>end;<br>
 
多人接受答案了。
 
顶部