linkedin facebook linkedin facebook nod32

Rekursiyadan foydalanib fayllarni izlash misol

Muallif: Mengliyev SH.

Qo`shilgan sana: 2014-09-16

Rekursiyadan foydalanib fayllarni izlash misol

    Misol tariqasida fayllarni izlash masalasiga rekursiya qo‘llashni ko‘rib chiqamiz. Foydalanuvchi ko‘rsatgan katalog va uning ost kataloglarida  masalan bmp kengaytmali hama fayllar topilsin.
Bu masalani hal qilish algoritmini kuyidagicha ta’riflash mumkin:
1.Berilgan shartga mos hamma fayllar ro‘yxati chiqarilsin.
2.Agar katalogda ostki katalog mavjud bo‘lsa u ham shunday ko‘rib chikilsin.

Fayl (Edit1) maydoni fayl nomini kiritish uchun mo‘ljallangan. Katalog nomi Papka maydoniga kiritiladi yoki Obzor papok, dialog oynasi yordamida tanlanadi. Bu dialog oynasi SelectDirectory standart funksiyasi yordamida chiqariladi. Oddiy satrni WideChar turiga aylantirish uchun StringToWhideChar funksiyasidan foydalanilgan.

Asosiy vazifani Find rekursiv funksiyasi bajaradi. Find funksiyasida bitta parametr searchRec strukturasi ishlatiladi. Birinchi va keyingi fayllarni izlash uchun FindFirst va FindNext funksiyalaridan foydalaniladi.

Dastur matni

unit Unit1;

interface
uses
Windows, Messages, SysUtils, Variants,
Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;FileCtr;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;

Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
    Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
FileName: string;
cDir: string;
n: integer;
procedure Find;
var
SearchRec: TSearchRec;
begin
GetDir(0,cDir);
if cDir [length (cDir) ] <> 'V' then cDir := cDir+'\';
if FindFirst(FileName, faArchive,SearchRec) = 0
then repeat
if (SearchRec.Attr and faAnyFile) = SearchRec.Attr
then begin
Form1.Memo1.Lines.Add(cDir + SearchRec.Name);
n := n + 1; end; until FindNext(SearchRec) <> 0;
if FindFirst('*', faDirectory, SearchRec) = 0 then repeat
if (SearchRec.Attr and faDirectory) = SearchRec.Attr then begin
if SearchRec.Name[1] <> '.' then begin
ChDir(SearchRec.Name);
Find;
ChDir('..');
end;
end;
until FindNext(SearchRec) <> 0;
end;
function GetPath(mes: string):string;
var
Root: string;
pwRoot : PWideChar; Dir: string;
begin
Root := '';
GetMem(pwRoot, (Length(Root)+1) * 2);
pwRoot := StringToWideChar(Root, pwRoot, MAX_PATH*2);
if SelectDirectory(mes, pwRoot, Dir) then
if length(Dir) =2
then GetPath := Dir+'\' else GetPath := Dir else
GetPath:= '';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
Label4.Caption := '';
FileName := Edit1.Text;
cDir := Edit2.Text;
n:=0;
ChDir(cDir);
Find;
if n = 0 then
ShowMessage('SHartga mos keluvchi fayylar yuk.')
else Label4.Caption := 'Fayllar soni:' + IntToStr(n);
end;
procedure TForm1.Button2Click (Sender: TObject);
var
Path: string; begin
Path := GetPath('Papkani tanlang');
if Path <> ''
then Edit2.Text := Path;
end;
end.

1651 marta o`qildi.

Parol:
Eslab qolish.


Ro`yhatdan o`tish

testing

+998915878681

Siz o`z maxsulotingizni 3D reklama ko`rinishda bo`lishini xohlaysizmi? Unda xamkorlik qilamiz.

3D Reklama


Рейтинг@Mail.ru
Рейтинг@Mail.ru

Besucherzahler
счетчик посещений