Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Comparar dos archivos entre sí y averiguar si son iguales (https://www.clubdelphi.com/foros/showthread.php?t=80416)

dec 07-06-2006 19:57:07

Comparar dos archivos entre sí y averiguar si son iguales
 
Esta función debería haberse incluido dentro del truco: Comparar dos Streams entre sí y averiguar si son iguales.

Nótese que utilizamos la función "CompararStreams" dentro de la función "CompararArchivos". En definitiva se trata de averiguar si dos determinados archivos son iguales o no:

Código Delphi [-]
uses
  Classes;

function CompararStreams(AStream1, AStream2: TStream): boolean;
var
  Buff1, Buff2: Pointer;
  iCount: LongInt;
const
  BUFFER_SIZE = 1024*4;
begin
  if AStream1.Size <> AStream2.Size then
    Result := False
  else
  begin
    Result := True;
    GetMem(Buff1, BUFFER_SIZE);
    GetMem(Buff2, BUFFER_SIZE);
    try
      repeat
        iCount := AStream1.Read(Buff1^, BUFFER_SIZE);
        AStream2.Read(Buff2^, BUFFER_SIZE);
        if not CompareMem(Buff1, Buff2, iCount) then
        begin
          Result := False;
          break;
        end;
      until iCount < BUFFER_SIZE;
    finally
      FreeMem(Buff1);
      FreeMem(Buff2);
    end;
  end;
end;

function CompararArchivos(AFileName1, AFileName2: TFileName): boolean;
var
  FS1, FS2: TFileStream;
begin
  FS1 := TFileStream.Create(AFileName1, fmOpenRead + fmShareDenyNone);
  try
    FS2 := TFileStream.Create(AFileName2, fmOpenRead + fmShareDenyNone);
    try
      Result := CompararStreams(FS1, FS2);
    finally
      FS2.Free;
    end;
  finally
    FS1.Free;
  end;
end;

Wellnic 06-05-2007 00:54:23

Otro manera de comparar dos archivos es escribir:

fc/b archivo1 archvo2

en la línea de comandos. :-)


La franja horaria es GMT +2. Ahora son las 14:33:16.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi