Board index » delphi » D1: What am I doing wrong?

D1: What am I doing wrong?

Hi,

I wrote the code below to copy all files of a directory to another
directory. I tested the code on a directory with 4 files in it.
Somehow the code only copies 2 of the 4 files.

procedure CopyFile( Const sourcefilename, targetfilename : String );
var
        Stream : TMemoryStream;
begin
        try
        Stream := TMemoryStream.Create;
        Stream.LoadFromFile(sourcefilename);
        Stream.SaveToFile(targetfilename);
        Stream.Free;
        except
                on E : Exception do
                        begin
                        raise Exception.Create('Error copying file: '
+E.Message);
                        end;
        end;
end;

procedure TMainForm.CopyClick(Sender: TObject);
begin
  TargetDir := Ausgabe.Text;
  SourceDir := Eingabe.Text;
  FindFirst(Eingabe.Text + '*.*', faAnyFile, SearchRec);
  Target := TargetDir + SearchRec.Name;
  Source := SourceDir + SearchRec.Name;
  if (SearchRec.Size > 0) then
      CopyFile(Source,Target);
  While FindNext(SearchRec) = 0 do
   begin
    FindNext(SearchRec);
    Target := TargetDir + SearchRec.Name;
    Source := SourceDir + SearchRec.Name;
    if (SearchRec.Size > 0) then
      CopyFile(Source,Target);
   end;
   FindClose(SearchRec);
end;

Oliver Fechner
hun...@fechner.d.eunet.de

 

Re:D1: What am I doing wrong?


Quote
Oliver Fechner wrote:

> Hi,

> I wrote the code below to copy all files of a directory to another
> directory. I tested the code on a directory with 4 files in it.
> Somehow the code only copies 2 of the 4 files.

        This is because half of the time when you find a file you
throw it away without doing anything with it and move on to the
next file:

[...]

Quote
>   While FindNext(SearchRec) = 0 do //we just found one
>    begin
>     FindNext(SearchRec); //now we found another one.

[...]
--
David Ullrich

sig.txt not found

Other Threads