I have an application that logs information in a daily text file every second on the host PC. A slave PC on the network using the same application would like to copy this text file to a local drive. I see that there will be problems with access to files.
These files should not exceed 30-40 MB. the network will be 100 MB ethernet. I see that there is a chance that the copy process will take more than 1 second, which means that the journal computer will need to open the file for writing while it is reading.
What is the best method for writing files (logging) and copying files? I know that there is a standard Windows CopyFile () procedure, however this gave me problems accessing files. There is also a TFileStream using the fmShareDenyNone flag, but this also very rarely gives me an access problem (e.g. 1 time per week).
What is the best way to accomplish this task?
My current file registration:
procedure FSWriteline(Filename,Header,s : String);
var LogFile : TFileStream;
line : String;
begin
if not FileExists(filename) then
begin
LogFile := TFileStream.Create(FileName, fmCreate or fmShareDenyNone);
try
LogFile.Seek(0,soFromEnd);
line := Header +
LogFile.Write(line[1],Length(line));
line := s +
LogFile.Write(line[1],Length(line));
finally
logfile.Free;
end;
end else begin
line := s +
Logfile:=tfilestream.Create(Filename,fmOpenWrite or fmShareDenyNone);
try
logfile.Seek(0,soFromEnd);
Logfile.Write(line[1], length(line));
finally
Logfile.free;
end;
end;
end;
My file copy procedure:
procedure DoCopy(infile, Outfile : String);
begin
ForceDirectories(ExtractFilePath(outfile));
if FileAge(inFile) = FileAge(OutFile) then Exit;
try
{ Open existing destination }
fo := TFileStream.Create(Outfile, fmOpenReadWrite or fmShareDenyNone);
fo.Position := 0;
except
{ otherwise Create destination }
fo := TFileStream.Create(OutFile, fmCreate or fmShareDenyNone);
end;
try
{ open source }
fi := TFileStream.Create(InFile, fmOpenRead or fmShareDenyNone);
try
cnt:= 0;
fi.Position := cnt;
max := fi.Size;
{start copying }
Repeat
dod := BLOCKSIZE;
if cnt+dod>max then dod := max-cnt;
if dod>0 then did := fo.CopyFrom(fi, dod);
cnt:=cnt+did;
Percent := Round(Cnt/Max*100);
until (dod=0)
finally
fi.free;
end;
finally
fo.free;
end;
end;
source
share