Title: Message
Tested the question below - a file has same number on NTFS and FAT32
 
I am using code like this as alternative way to copy a file using FileStream.
Like it that the copy is done in one line using CopyFrom :) - the rest is just housekeeping and checking.
 
(got some ideas from Google- as I could not find your example in the D2006 help)
 
so far seems to work fine...if anyone has suggestions for improvement add them.
 
function xffsCopyFile(aFile1:string;aFile2:string):int64;
//returns number bytes copied, -1 if not successful
var
FileSize1,FileSize2,bytescopied:int64;
fs1,fs2:TFileStream;
FileAge1,FileAge2:integer;
begin
 result:=0;
  bytescopied:=0;
  fs1 := TFileStream.Create(aFile1, fmOpenRead);
  try
    fs2 := TFileStream.Create(aFile2, fmCreate);
    try
      {Forget about block reads and writes, just copy
       the whole darn thing.}
      bytescopied:=fs2.CopyFrom(fs1, 0);
    finally
      fs2.Free;
    end;
  finally
    fs1.Free;
  end;
  result:=bytescopied;
  //alter file dateTime Stamp to match original file
  if (FileExists(aFile1))
  and (FileExists(aFile2)) then
  begin
    FileSize1:=xffsFileSize(aFile1);
    FileSize2:=xffsFileSize(aFile2);
    FileAge1:=FileAge(aFile1);
    FileAge2:=FileAge(aFile2);
    if (FileSize1=FileSize2)
      and(FileSize2=bytescopied)
      and(FileAge2>FileAge1) then
      begin
       //only set date if copied right size and date of
        //File2 now later than File1
      FileSetDate(aFile2,FileAge1);
      end
      else result:=-1;  //signal error
  end;
end;
 
function xffsFileSize(Filename:string):int64;
var
fs:TFileStream;
begin
 result:=0;
  if FIleExists(Filename) then
  begin
    try
      fs:=Tfilestream.create(FileName,fmOpenRead or fmShareDenyNone);
      //more code for I/U
      fs.seek(0,soFromBeginning);
      result:=fs.size;
    finally
    fs.free;
    end;
  end;
end;

John Bird

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of John Bird
Sent: Friday, 22 September 2006 12:07 p.m.
To: 'Conor Boyd'; 'NZ Borland Developers Group - Delphi List'
Subject: RE: [DUG] Copying files problem

If I use FileSetDate to set the date/time of the destination file, using the value I got for the source file (calling eg FileAge) does it set the same date/time if the source and destination file systems are different types (eg FAT32/NTFS) ? 
 
Anyone know off the top of their head if this number is filesystem dependent or not?
 
 

John

-----Original Message-----
From: Conor Boyd [mailto:[EMAIL PROTECTED]
Sent: Friday, 22 September 2006 10:51 a.m.
To: [EMAIL PROTECTED]; NZ Borland Developers Group - Delphi List
Subject: RE: [DUG] Copying files problem

You could still use the FileStream technique and just touch the time stamps as required using FileSetDate in SysUtils?
 
C.


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of John Bird
 
The main reason I wanted to use an API method (ie copying a file rather than using FileStream etc to copy the data) is I want to preserve the date and time filestamps on the files, as I do a smart copy - only copying files that have a later modification date.

[snip]



__________ NOD32 1.1461 (20060329) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com
_______________________________________________
Delphi mailing list
[email protected]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to