unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, ToolEdit;

type
  TForm1 = class(TForm)
    DirectoryEdit1: TDirectoryEdit;
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Label1: TLabel;
    Label2: TLabel;
    SaveDialog1: TSaveDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses HashUnit;

{$R *.dfm}

function SlashSep(const Path, S: string): string;
begin
  if AnsiLastChar(Path)^ <> '\' then
    Result := Path + '\' + S
  else
    Result := Path + S;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  RootPath: string;

  procedure DoMD5(RelativePath: string='');
  var
    DirName: string;
    Status: Integer;
    SearchRec: TSearchRec;
    sMD5: string;
  begin
    if RelativePath <> '' then
      DirName := SlashSep(RootPath, RelativePath)
    else
      DirName := RootPath;
    try
      Status := FindFirst(SlashSep(DirName, '*.*'), faArchive, SearchRec);
      while Status = 0 do
      begin
        if (SearchRec.Attr and faArchive = faArchive) then
        begin
          sMD5 := MD5DigestToStr(MD5File(SlashSep(DirName, SearchRec.Name)));
          if RelativePath<>'' then
            Memo1.Lines.Add(SlashSep(RelativePath, SearchRec.Name)+#9+sMD5)
          else
            Memo1.Lines.Add(SearchRec.Name+#9+sMD5)
//          setAttribute('name', SearchRec.Name);
//          setAttribute('size', inttostr(SearchRec.Size));
        end;
        Status := FindNext(SearchRec);
      end;
    finally
      FindClose(SearchRec);
    end;

    try
      Status := FindFirst(SlashSep(DirName, '*.*'), faDirectory, SearchRec);
      while Status = 0 do
      begin
        if (SearchRec.Attr and faDirectory = faDirectory) then
          if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
          begin
            if RelativePath<>'' then
              DoMD5(SlashSep(RelativePath, SearchRec.Name))
            else
              DoMD5(SearchRec.Name)
          end;
        Status := FindNext(SearchRec);
      end;
    finally
      FindClose(SearchRec);
    end;
  end;
begin
  if DirectoryEdit1.Text='' then
    raise Exception.Create('ÇëÑ¡Ôñ´¦ÀíÄ¿Â¼');
  RootPath := DirectoryEdit1.LongName;
  DoMD5;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
    memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;

end.
