怎样实现文件的查找并删除?(50分)

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

cubstars

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位高手:我想查找计算机内的所有以BAK为扩展名的文件并将其删除,应该怎么来实现?请给出相关代码!先谢了……
 
找一个目录下的文件
procedure TForm1.SearchClick(Sender: TObject);
var
SearchRec: TSearchRec;
begin
FindFirst('c:/*.*', faAnyFile, SearchRec);
form1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes ';

end;
procedure TForm1.searchagainClick(Sender: TObject);
begin
if (FindNext(SearchRec) = 0)
showmessage('found it again!');

else
FindClose(SearchRec);
end;
删除用deletefile
 
如果是查找文件名,有API,删除也一样,如果需要查找子目录,我有例子
 
首先谢谢您!
如果我把这个代码加到.FormCreate事件里,而且我并不知道要查找并删除的文件在哪里,
改怎么办?
 
按你的问题内容,应该进行以下几步操作:
1. 确认系统有几个物理盘符
2. 对每个盘符进行文件遍历,对于文件如果是bak文件则进行删除,不是则继续;
如果是子目录,则需要对子目录进行文件遍历。

程序实现是简单的递归方式,算法很简单,网上多处可以找到类似的源代码。
需要注意的是:最好能将查到的符合结果的文件进行列表,由操作员再次确认再删除。
visio2002中有一data.bak文件,但是在安装过程中这个文件会多处使用。不能盲目删除。
 
我贴一段别人的
你自己看修改一下
unit MainFrm;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, FileCtrl, Grids, Outline, DirOutln, ComCtrls;

type
TMainForm = class(TForm)
dcbDrives: TDriveComboBox;

edtFileMask: TEdit;
lblFileMask: TLabel;
btnSearchForFiles: TButton;
lbFiles: TListBox;
dolDirectories: TDirectoryOutline;
RichEdit1: TRichEdit;
Button1: TButton;

procedure btnSearchForFilesClick(Sender: TObject);
procedure dcbDrivesChange(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
FFileName: String;
function GetDirectoryName(Dir: String): String;
procedure FindFiles(APath: String);
end;

var
MainForm: TMainForm;

implementation

{$R *.DFM}

function TMainForm.GetDirectoryName(Dir: String): String;
{ This function formats the directory name so that it is a valid
directory containing the back-slash (/) as the last character. }
begin
if Dir[Length(Dir)]<> '/' then
Result := Dir+'/'
else
Result := Dir;
end;

procedure TMainForm.FindFiles(APath: String);
{ This is a procedure which is called recursively so that it finds the
file with a specified mask through the current directory and its
sub-directories. }
var
FSearchRec,
DSearchRec: TSearchRec;
FindResult: integer;
FindResult2: integer;
function IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;

begin
APath := GetDirectoryName(APath); // Obtain a valid directory name
{ Find the first occurence of the specified file name }
FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+
faSysFile+faReadOnly,FSearchRec);
//new add
// FindResult2 := FindFist(Apath );

lbFiles.Items.Add(LowerCase(APath));
try
{ 继续查找文件,查到加到 ListBox }
while FindResult = 0 do
begin
lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name));
FindResult := FindNext(FSearchRec);
end;

{ Now search the sub-directories of this current directory. Do this
by using FindFirst to loop through each subdirectory, then call
FindFiles (this function) again. This recursive process will
continue until all sub-directories have been searched. }
FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec);

while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and not
IsDirNotation(DSearchRec.Name) then
FindFiles(APath+DSearchRec.Name); // Recursion here
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
end;
end;

procedure TMainForm.btnSearchForFilesClick(Sender: TObject);
{ This method starts the searching process. It first changes the cursor
to an hourglass since the process may take awhile. It then clears the
listbox and calls the FindFiles() function which will be called
recursively to search through sub-directories }
begin
Screen.Cursor := crHourGlass;
try
lbFiles.Items.Clear;
FFileName := edtFileMask.Text;
FindFiles(dolDirectories.Directory);
finally
Screen.Cursor := crDefault;
end;
end;

procedure TMainForm.dcbDrivesChange(Sender: TObject);
begin
dolDirectories.Drive := dcbDrives.Drive;
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
try
lbFiles.Items.Clear;
// richedit1.Clear;
FFileName := '*.*';
FindFiles('d:/kaoshi/b/');
finally
Screen.Cursor := crDefault;
end;
end;

end.
 
后退
顶部