Home > Back-end >  Delphi realize drive specified file search filtering capabilities
Delphi realize drive specified file search filtering capabilities

Time:09-27



The unit Unit1;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls FileCtrl;

Type
TForm1=class (TForm)
DriveComboBox1: TDriveComboBox;
DirectoryListBox1: TDirectoryListBox;
Edit1: TEdit;
For: TButton;
Button2: TButton;
FileListBox1: TFileListBox;
FilterComboBox1: TFilterComboBox;
Procedure DriveComboBox1Change (Sender: TObject);
Procedure DirectoryListBox1Change (Sender: TObject);
Procedure FileListBox1Click (Sender: TObject);
Procedure Button1Click (Sender: TObject);
Procedure FileListBox1Change (Sender: TObject);
Procedure Button2Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
The end;

Var
Form1: TForm1;

Implementation

{$R *. DFM}

Procedure TForm1. DriveComboBox1Change (Sender: TObject);
The begin
Form1. DirectoryListBox1. Drive:=Form1 DriveComboBox1. Drive;//to monitor the changes of DriveComboBox
The end;

Procedure TForm1. DirectoryListBox1Change (Sender: TObject);
The begin
Form1. FileListBox1. Directory:=Form1 DirectoryListBox1. Directory;//to monitor the changes of DirectoryListBox
The end;

Procedure TForm1. FileListBox1Click (Sender: TObject);
The begin
Edit1. Text:=filelistbox1. FileName;//Edit text box to add the filename
The end;
Procedure TForm1. Button1Click (Sender: TObject);//delete the folder
The begin
The end;

Procedure TForm1. FileListBox1Change (Sender: TObject);
The begin
Edit1. Text:=filelistbox1. Directory;//Edit text box to add the file path
The end;

Procedure TForm1. Button2Click (Sender: TObject);
The begin

The end;

end.

CodePudding user response:

What problem? The file is a query?
Recursive search directory file directly by the,


Delphi the general method of recursive search directory under

//define the callback function search to file
//if the wrong object method, please remove of object part
//aFile: search the file
//willStop: external variables, is used to determine the callback after the operation, whether to terminate the search,
TFindFile=procedure (aFile: string; Var willStop: Boolean) of the object;

//generic directory search algorithm
//aDir: to search the directory
//onFind: search to file the callback function when
Procedure doFindFile (aDir: string; OnFind: TFindFile);
Var strDir strfile: string;
Ff: _WIN32_FIND_DATAA;
Hf: THandle;
BlStop: Boolean;
The begin
//record the current directory
StrDir:=getCurrentDir;
//set the current directory to search the directory
SetcurrentDir (aDir);
Try
//search
Hf:=Windows. The FindFirstFile (' *. *, ff);
If hf & gt; 0 then the begin
Repeat
StrFile:=ff. CFileName;
//if it is a directory, then the recursive call
If (ff) dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY=FILE_ATTRIBUTE_DIRECTORY) then the begin
If (strFile & lt;> ") and (pos ('. ', strFile)=0) then the begin
DoFindFile (aDir + strFile + '/', onFind);
The end;
End the else begin
BlStop:=False;
//if the file, refer to the callback function
OnFind (aDir + strFile blStop);
//if the callback function demanded an end to the search, then exit the current recursive process
If blStop then begin
Windows. FindClose (hf);
The Exit;
The end;
The end;
Until (not Windows. FindNextFile (hf, ff));
//termination search
Windows. FindClose (hf);
The end;
The finally
//resume search directory before
SetCurrentDir (strDir);
The end;
The end;

Algorithm is the key by defining a generic parameter of the callback function, the key to the directory search operating independence, in order to realize the algorithm of generality,
The following example shows that the calling process:

Procedure TForm1. FindTextFile (aFile: string; Var willStop: Boolean);
The begin
If LowerCase (ExtractFileExt aFile ())='. TXT 'then
WillStop:=Application. MessageBox (pchar (' found text file '+ aFile +'! To stop the search? '), 'prompt', MB_YESNO)=mrYes;
The end;

Procedure TForm1. Button1Click (Sender: TObject);
The begin
//Edit1. Text defines to search the directory
If the Copy (Edit1. Text, Length (Edit1. Text), 1) & lt;> '/' then Edit1. Text:=Edit1. Text + '/';
//call the generic search algorithm, the search text file
DoFindFile (Edit1. Text, findTextFile);
The end;

CodePudding user response:

Is to specify the drive letter or file directory specified extensions file query, delete operation
  • Related