Tìm kiếm file và folder bằng ngôn ngữ Pascal Thủ tục FindFirst procedure FindFirstPath: PChar; Attr: Word; var F: TSearchRec; Trong đó: + Path có kiểu PChar kiểu con trỏ đến một chuỗi n
Trang 1Tìm kiếm file và folder bằng ngôn ngữ Pascal
Thủ tục FindFirst
procedure FindFirst(Path: PChar; Attr: Word; var 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ằng Giá trị
ReadOnly
Hidden
SysFile
VolumeID
Directory
Archive
AnyFile
$01
$02
$04
$08
$10
$20
$3F
Trang 2+ 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:
Mã Nghĩa
2 Không tìm thấy file
Trang 33 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
Trang 4
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;
Trang 5END;
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);
Trang 6tmp^.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
Trang 7pop(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);
Trang 8END;
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