Thứ Hai, 5 tháng 3, 2012

Tìm kiếm file và folder bằng ngôn ngữ Pascal


Tìm kiếm file/folder trong Pascal bằng cách sử dụng thủ tục FindFirst và FindNext trong thư viện Dos.

Thủ tục FindFirst
procedure FindFirst(Path: PChar; Attr: Wordvar F: TSearchRec);
Trong đó:
+ Path có kiểu PChar (kiểu con trỏ đến một chuỗi) nhưng ở đây bạn có thể truyền vào một chuỗi kiểu string, ví dụ: bạn cần tìm tất cả các file đuôi .exe trong thư mục C:\Windows thì bạn cần truyền vào chuỗi sau: ‘C:\Windows\*.exe’
+ Attr là thuộc tính của file/folder được định sẵn bằng các hằng trong thư viện DOS:
HằngGiá trị
ReadOnly
Hidden
SysFile
VolumeID
Directory
Archive
AnyFile
$01
$02
$04
$08
$10
$20
$3F
+ F là tham biến kiểu TSearchRec chứa kết quả tìm được, TSearchRec là kiểu record được định nghĩa như sau:
type
  TSearchRec = record
    Fill: array[1..21] of Byte;
    Attr: Byte;
    Time: Longint;
    Size: Longint;
    Name: array[0..12] of Char;
  end;
Thủ tục FindNext:
procedure FindNext(var F: TSearchRec);
Trong đó biến F chứa thông tin phục vụ cho việc tiếp tục tìm kiếm theo thủ tục FindFirst đã gọi trước đó.
Nếu tìm kiếm thất bại thì biến DosError sẽ được mang giá trị khác 0 như sau:
Nghĩa
2
Không tìm thấy file
3
Không tìm thấy đường dẫn
5
Truy cập bị cấm
18
Đã tìm hết
Chương trình minh họa: liệt kê tất cả các file .PAS trong thư mục hiện hành.
USES Dos;

VAR
 DirInfo: SearchRec;
BEGIN
 FindFirst('*.PAS', AnyFile, DirInfo){ Same as DIR *.PAS }
 WHILE DosError = 0 DO
 BEGIN
   Writeln(DirInfo.Name);
   FindNext(DirInfo);
 END;
 readln;
END.

Chương trình liệt kê tất cả các file trong máy tính bằng cách sử dụng 2 thủ tục trên cùng với một Stack. Lưu ý: chỉ chạy tốt với Free Pascal, trên Turbo Pascal chương trình bị lỗi khi gặp đường dẫn thư mục quá dài.
USES Dos, crt;

TYPE pNode = ^Node;
     Node = RECORD
     s: STRING;
     pNext: pNode;
     END;

VAR pHead: pNode; {Con tro den dau stack}
    found: SearchRec;
    drive: CHAR;
    findPath: STRING;
    count: LONGINT;
    fo: TEXT;
PROCEDURE InitStack;
BEGIN
    pHead:=NIL;
END;

FUNCTION isStackEmpty: BOOLEAN;
BEGIN
     isStackEmpty:= (pHead = NIL);
END;                                                                                                              

PROCEDURE Pop(VAR s:STRING);
VAR tmp: pNode;
BEGIN
     s:=pHead^.s;
     tmp:=pHead;
     pHead:=pHead^.pNext;
     dispose(tmp);
END;

PROCEDURE Push(s:STRING);
VAR tmp:pNode;
BEGIN
     new(tmp);
     tmp^.s:=s;
     tmp^.pNext:=pHead;
     pHead:=tmp;
END;

BEGIN
 assign(fo,'AllFiles.txt'); rewrite(fo);

 InitStack;
 count:=0;

 {Them tat ca cac o dia tu A den Z vao stack}
 FOR drive:='z' DOWNTO 'a' DO Push(drive + ':');

 writeln('Ready to listing, while listing you can press any key to stop');
 writeln('Press Enter...');
 readln;

 WHILE NOT isStackEmpty DO
 BEGIN
      pop(findPath);

      FindFirst(findPath + '\*.*', AnyFile, found);
      IF DosError <> 0 THEN continue;
      inc(count);
      writeln(count);
      IF KeyPressed THEN break;

      writeln(fo, findPath);
      WHILE DosError = 0 DO
      BEGIN
          IF found.Attr AND Directory <> 0 THEN
          BEGIN
             IF (found.Name<>'.') AND (found.Name<>'..') THEN
                 Push(findPath + '\' + found.Name)
          END
          ELSE
          BEGIN
              inc(count);
              Writeln(fo, findpath + '\' + found.Name);
          END;
          FindNext(found);
      END;
   END;
   writeln(fo, 'There are ',count, ' files and folders in this computer!');
   close(fo);
   writeln('Done!');
   writeln('There are ',count, ' files and folders in this computer!');
   readln;
END.


Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 nhận xét

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© Download do an khoa luan tai lieu
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0
Posts RSSComments RSS
Back to top