-1

When I create a textfile, next copy it to another directory and then try to delete the original, it won't work, because the programa keeps the file locked. Before deleting the file, I set the file-attribute to 'normal'like this:

SetFileAttributes((pchar('C:\test')),FILE_ATTRIBUTE_NORMAL);

I cannot find any simple solution to resolve this. I create the file like this:

    bestand:= tstringlist.Create;
    try
      r:= FindFirst('test.*', faAnyFile, Res);
      try
       EOFound:= False;
        if (r = 0) then
          while not EOFound do
            begin
              bestand.Add(res.Name);
              EOFound:= FindNext(Res) <> 0;
            end;
      finally
        FindClose(Res);
      end;
    finally
      bestand.SaveToFile('C:\test');
      bestand.Free;
    end;

The same problem occurs when only reading the file like this:

AssignFile(Txt,TmpBest);
Reset(Txt);
  while not Eof(Txt) do
     begin
        Readln(Txt, s);
        L.Items.add.caption:=s;
     end;
  CloseFile(Txt);

Later, I set the file attributes to 'Normal' and try to delete the file:

  if CopyFile(pchar(file-org), pchar(file-dest), false) then 
    begin 
      SetFileAttributes(pchar(file-org),FILE_ATTRIBUTE_NORMAL); 
      if not DeleteFile(file-org) then 
        showmessage('delete ' + file-org + ' failed!'); 

where file-org is file Txt/TmpBest from the description above. I must say: I am not a Delphi programmer; I write in COBOL, but 'inherited' this Delphi-program from a former collegue and need to add some changes to it.

I.Hirs
  • 1
  • 1
  • Exactly how are you trying to copy and delete the file? Has your program existed/closed before you attempt it? – lurker Jul 10 '18 at 01:42
  • This is what I do: if CopyFile(pchar(file-org), pchar(file-dest), false) then begin SetFileAttributes(pchar(file-org),FILE_ATTRIBUTE_NORMAL); if not DeleteFile(file-org) then showmessage('delete ' + file-org + ' failed!'); where file-org is file Txt/TmpBest from the description above. I am not a Delphi programmer; I write in COBOL, but 'inherited' this Delphi-program from a former collegue and need to add some changes to it. – I.Hirs Jul 10 '18 at 08:37
  • Please edit your question and add your properly formatted code there. Comments are not the place to include code. Otherwise other readers may not find it and it's harder to read. – lurker Jul 10 '18 at 09:41
  • On any current Windows file system I know, normal users will not have write access to "C:\" - Are you sure your program doesn't really fail much earlier? – tofro Jul 10 '18 at 14:46
  • After closefile filesystem locks might linger for tens of ms. This is normal Windows behaviour and not related to Pascal or Cobol. Insert a small delay, and it will probably just work. – Marco van de Voort Jul 11 '18 at 10:50

1 Answers1

0

I found the answer to my own question. I already mentioned that I am not a Delphi-programmer, so I did not notice another left-over statement from before my changes:

FSource := TFileStream.Create(SourceFile, fmOpenRead or fmShareDenyNone);

Removing that statement solved my problem; obviously that statement locked my file until close of the program. Thanks for your trying to help anyway.

I.Hirs
  • 1
  • 1