Thursday, May 14th, 2009 | Author: Riza

Code berikut adalah rutin program untuk melakukan search seluruh file dan subdirektori yang ada di dalam suatu drive atau direktori tertentu dan ditulis dalam bahasa pemrograman Delphi (object pascal). Searching dilakukan dengan teknik rekursi (recursion).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
program RecursiveSearch;
 
{$APPTYPE CONSOLE}
 
uses
  SysUtils,
  Windows;
 
procedure GotoXY(AX, AY: Integer);
var
  point: COORD;
  hConsole: THandle;
begin
  point.X := 0;
  point.Y := 0;
  hConsole := GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleCursorPosition(hConsole, point);
end;
 
procedure ClearScreen;
var
  maxSize, point: COORD;
  numberOfCharsWritten: Cardinal;
  hConsole: THandle;
begin
  hConsole := GetStdHandle(STD_OUTPUT_HANDLE);
  maxSize := GetLargestConsoleWindowSize(hConsole);
  point.X := 0;
  point.Y := 0;
  FillConsoleOutputCharacter(hConsole, ' ', maxSize.X*maxSize.Y, point,
    numberOfCharsWritten);
end;
 
procedure WriteToScreen(AFileName: string);
begin
  ClearScreen();
  GotoXY(0, 0);
  Writeln('Searching:');
  Writeln(AFileName);
end;
 
procedure FileSearch(AFileSpec: string);
var
  searchRecord: TSearchRec;
  path, fileName, fullName: string;
  isDirectory: Boolean;
begin
  path := ExtractFilePath(AFileSpec);
  fileName := ExtractFileName(AFileSpec);
 
  if (FindFirst(AFileSpec, faAnyFile, searchRecord) <> 0) then
    Exit;
 
  while FindNext(searchRecord) = 0 do
  begin
    if searchRecord.Name[1] <> '.' then
    begin
      fullName := path + LowerCase(searchRecord.Name);
 
      { Anda bisa menambahkan rutin untuk melakukan sesuatu di sini. Misalnya
        menampilkan file yang di search pada layar. }
      WriteToScreen(fullName);
 
      isDirectory := searchRecord.Attr and faDirectory > 0;
      if isDirectory then
        FileSearch(fullName+'\'+fileName);
    end;
  end;
  Sleep(10);
end;
 
begin
  FileSearch('c:\*.*');
  Writeln('Finish.');
  Readln;
end.

Pada implemetasinya, code program di atas akan malakukan search seluruh file yang ada pada drive c. Sesuai dengan masukan yang diberikan untuk procedure FileSearch(), yaitu ‘c:\*.*’.

Semoga bermanfaat. ***

Category: Delphi, Programming
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses

  1. salam kenal,
    kalo metode pencarian file yang ada di server, gimana yaa mas ??. servernya tidak disharing, tapi by http://FTP. mohon pencerahannya.
    makasi sebelumnya

  2. @komink: harusnya prinsipnya sama saja. Hanya harus menambah satu class untuk memanage proses login, logout, ke server ftp-nya. kalau developnya pakai delphi, coba komponen ftp yang sudah disediakan di components palette di bagian Indy Server. Delphi sudah menyediakan komponen untuk melakukan proses ke server http://ftp.

Leave a Reply » Log in


Verification: