[DUG] Copying files problem
John Bird
johnkbird at paradise.net.nz
Tue Sep 26 19:01:06 NZST 2006
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: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz] 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:Conor.Boyd at trimble.co.nz]
Sent: Friday, 22 September 2006 10:51 a.m.
To: johnkbird at paradise.net.nz; 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: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz] 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ns3.123.co.nz/pipermail/delphi/attachments/20060926/eeafd242/attachment-0001.html
More information about the Delphi
mailing list