1

I am struggling to find an answer to the following problem. Any and all help would be appreciated.

I am using the following code to try and copy an outlook.pst file while outlook is open. And i cannot get it to succeed. It does not give an error, it just doesnt copy the file.

copyfile('C:\Users\Administrator\Documents\Outlook Files\Outlook.pst','F:\Outlook.pst');

If you guys know how i will be able to copy a locked file like that please assist.

I have tried and found that TFilestream also does not work.

And those 2 are the only options i know off. any help would be greatly appreciated.

Thank You

I have tried the following code as-well and get an error saying that the file is in use from another process(outlook).

procedure TForm1.Button2Click(Sender: TObject);
var
   NewFileName: string;
   NewFile: TFileStream;
   OldFile: TFileStream;
Begin
           NewFileName:='F:\outlook.pst';
           OldFile := TFileStream.Create('C:\Users\Administrator\Documents\Outlook Files\outlook.pst', fmOpenRead or fmShareDenyWrite);
            try
              NewFile := TFileStream.Create(NewFileName, fmCreate or fmShareDenyNone);
              try
                NewFile.CopyFrom(OldFile, OldFile.Size);
              finally
                FreeAndNil(NewFile);
              end;
            finally
              FreeAndNil(OldFile);
            end;
end;

Please see the following link. If anybody can convert the code. the problem should be solved. How to copy a pst file while it is open using c#

Community
  • 1
  • 1
user3271392
  • 149
  • 2
  • 12

2 Answers2

2

PST provider locks PST files until the parent process terminates. Even if you close the PST file from Outlook, it will be kept open for 30 minutes for the performance reasons.

Do you programmatically open the PST file in Outlook?

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

Try the fmShareDenyNone flag when creating the TFileStream object:

stream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone);
try 
   slFile.LoadFromStream(stream);
finally
   stream.Free;
end;

Function to read the date from a file:

function GetFileDate(TheFileName: string): string;
var
  FHandle: integer;
begin
  FHandle := FileOpen(TheFileName, fmShareDenyNone);
  try
    Result := DateTimeToStr(FileDateToDateTime(FileGetDate(FHandle)));
  finally
    FileClose(FHandle);
  end;
end;
Juliano
  • 821
  • 6
  • 21
  • Thanks i will give it a try and let you know. – user3271392 Feb 04 '14 at 15:39
  • Please check above for the changes. i included your code – user3271392 Feb 04 '14 at 15:48
  • 1
    I think you have to use fmShareDenyNone on the OldFile, instead of the NewFile. – Juliano Feb 04 '14 at 15:52
  • sorry my mistake, i adapted it. But i still get an error "Stream read error." when i execute the command – user3271392 Feb 04 '14 at 15:55
  • Have you tried using FileOpen and fmShareDenyNone? In this case you have to handle the copy manually, that is, you have to open the source and destination files, read the bytes from the source and write it to the destination using read/write. – Juliano Feb 04 '14 at 15:58
  • Im probably being stupid, but i lost you completely. Can you Post an example of what you mean? – user3271392 Feb 04 '14 at 16:00
  • 1
    I´ve edited the code to put an example about how to get the date from a file which can be opened at that moment. – Juliano Feb 04 '14 at 16:02