关于用Delphi 7 如何编写一个程序用来实现删除计算机中某一类文件?(100分)

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

wanda216

Unregistered / Unconfirmed
GUEST, unregistred user!
需要完整的代码,比如删除计算机中所有的log为结尾的文件,怎么实现?<br>用批处理来写是 del c:/*.log 保存为*.bat 后可以实现删除C盘下所有没有用的log文件.,但是我想用Delphi 来实现,不想用批处理来写.请问哪位朋友可以帮助我,帮我完成这个小程序.哪位能把源代码贴出来吗?
 
直接调用这种批处理命令多简单啊,不用自己去查找文件了:)
 
我想用程序去完成,不想调用~!
 
用查找并删除不就可以了!<br>findfirst,findnext,deletefile搞定!
 
TFindFileProc = procedure(const nowPath: string; SearchRec: TSearchRec) of<br> &nbsp; &nbsp;object; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>procedure FindFiles(path: string; DoProc: TFindFileProc; recursion:<br> &nbsp;boolean);<br>var<br> &nbsp;f &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TSearchRec;<br> &nbsp;i &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : integer;<br> &nbsp;tmp &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : string;<br>begin<br> &nbsp;if RightStr(path, 1) &lt;&gt; '/' then<br> &nbsp; &nbsp;path := path + '/';<br> &nbsp;i := FindFirst(path + '*.*', faAnyFile, f);<br> &nbsp;repeat<br> &nbsp; &nbsp;if (f.Name = '.') or (f.Name = '..') then<br> &nbsp; &nbsp; &nbsp;continue;<br> &nbsp; &nbsp;DoProc(path, f);<br> &nbsp; &nbsp;if ((f.Attr and fadirectory) &lt;&gt; 0) and recursion then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;tmp := f.Name + '/';<br> &nbsp; &nbsp; &nbsp;path := path + tmp;<br> &nbsp; &nbsp; &nbsp;FindFiles(path, DoProc, recursion);<br> &nbsp; &nbsp; &nbsp;Delete(path, ansipos(tmp, path), length(tmp));<br> &nbsp; &nbsp;end;<br> &nbsp;until FindNext(f) &lt;&gt; 0;<br> &nbsp;FindClose(f);<br>end;
 
给你个过程即可!<br>procedure delallfilesinpath(path:string); &nbsp; &nbsp; //删除目录下*.log<br>var<br> &nbsp;sr:tsearchrec;<br>begin<br> &nbsp;if findfirst(path+'/*.log',faanyfile,sr)=0 then<br> &nbsp;begin<br> &nbsp; &nbsp; deletefile(path+'/'+sr.name);<br> &nbsp;end ;<br> &nbsp;while findnext(sr)=0 do<br> begin<br> &nbsp; &nbsp; deletefile(path+'/'+sr.name);<br> &nbsp;end;<br>end;<br>或再给你一个比较全的!<br>procedure xfiles(dir: string);<br>var<br>i,Found:Integer;<br>sr: TSearchRec;<br>begin<br>Found:=FindFirst(dir+'/*.log',$10,sr);<br>while Found=0 do<br>begin<br>if (sr.Name='.') or (sr.Name='..') then<br> &nbsp;begin<br> &nbsp; Found := FindNext(sr);<br> &nbsp; continue;<br> &nbsp;end<br>else<br> &nbsp;begin<br> &nbsp; if &nbsp;not(sr.Attr and faDirectory &gt; 0) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;deletefile(dir+'/'+sr.Name);<br> &nbsp; &nbsp;end<br> &nbsp; else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;deletefile(dir+'/'+sr.Name);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>Found:= FindNext(sr);<br>end;<br>FindClose(sr);<br>end;<br>xfiles(c:/)删除C盘下的*.log包括子目录下的!
 
我的就一句<br>加下面一名就可以了xp/2000/2003/nt下是这样的一句 &nbsp;<br>winexec('cmd.exe /c del /q c:/*.log',sw_hide); 这是相对路径 程序的当前目录<br>98下就这样一句<br>winexec('command.com /c del /q c:/*.log',sw_hide);<br><br>完整代码如下:我在2000+d7下调的.<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>winexec('cmd.exe /c del /q c:/*.log',sw_hide);<br>end;<br><br>end.
 
楼主把分给我吧:<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure do_something(startdir,filename,status:string);<br>begin<br> &nbsp;if status='delete' then<br> &nbsp;begin<br> &nbsp; &nbsp;deletefile(startdir+filename);<br> &nbsp;end;<br>end;<br><br>procedure FindFiles(StartDir,searchfilename: string;searchsubdir:boolean;status:string);<br>var<br> &nbsp;SR: TSearchRec;<br> &nbsp;IsFound: Boolean;<br>begin<br> &nbsp;IsFound :=FindFirst(StartDir+searchfilename, faAnyFile-faDirectory, SR) = 0;<br> &nbsp;while IsFound do begin<br> &nbsp; &nbsp;do_something(startdir,sr.name,status);<br> &nbsp; &nbsp;IsFound := FindNext(SR) = 0;<br> &nbsp;end;<br> &nbsp;FindClose(SR);<br> &nbsp;if searchsubdir=false then exit;<br> &nbsp;IsFound := FindFirst(StartDir+'*.*', faAnyFile, SR) = 0;<br> &nbsp;while IsFound do begin<br> &nbsp; &nbsp;if ((SR.Attr and faDirectory) &lt;&gt; 0) and(SR.Name[1] &lt;&gt; '.') then findfiles(startdir+sr.Name+'/',searchfilename,true,status);<br> &nbsp; &nbsp;IsFound := FindNext(SR) = 0;<br> &nbsp;end;<br> &nbsp;FindClose(SR);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;path:string;<br>begin<br> &nbsp;path:='d:/'';<br> &nbsp;findfiles(path,'*.log',true,'delete');<br>end;<br><br>end.<br><br>这个程序是删除D:/下所有文件夹下的(包含所有的子目录)中的LOG文件,如果你不想包含子目录,则把findfiles(path,'*.log',true,'delete')这句的true改成false即可。
 
谢谢你的答案,我很满意!
 
后退
顶部