Delphi7 - How can I copy a file that is written to

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 + #13#10;
             LogFile.Write(line[1],Length(line));
             line := s + #13#10;
             LogFile.Write(line[1],Length(line));
          finally
                 logfile.Free;
          end;
     end else begin
         line := s + #13#10;
         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)); //ensure folder exists
     if FileAge(inFile) = FileAge(OutFile) then Exit; //they are the same modified time
     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; // Block size
                 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;
+3
source share
4 answers

I would suggest not closing and opening the shared file again and again. Since you write to him every second, this is just an extra overhead.

( fmCreate !), fmOpenWrite fmShareDenyWrite , .

fmOpenRead fmShareDenyNone , . . . , , . , , , .

+2

:

, Delphi .

TFileStream.Create() , , 2007 . concurrency.

, , , , ( ShareMode), concurrency.

, , , - , :

  if not FileExists(filename) then
  begin
    // There may be a more efficient way of creating an empty file, but this 
    //  illustrates the approach

    LogFile := TFileStream.Create(FileName, fmCreate);
    LogFile.Free;

    line := Header + #13#10 + s + #13#10;
  end
  else
    line := s + #13#10;

  Logfile:=tfilestream.Create(Filename,fmOpenWrite or fmShareDenyNone);
  try
    logfile.Seek(0,soFromEnd);
    Logfile.Write(line[1], length(line));
  finally
    Logfile.free;
  end;
+1

Append / , write close .

/ ; , .

Delphi, MoveFile, .

, , try-except, , (NTFS Windows?) concurrency. :

  • , .
  • , .

, /.

0

"IsFileInUse" - , , :

// master
while IsFileInUse(*AFileName*) do
  Sleep(10);
write-content-to-file

// slave
while IsFileInUse(*AFileName*) do
  Sleep(10);
copy-file-to-a-special-location

presto!! !

-1

Source: https://habr.com/ru/post/1785332/


All Articles