signalb has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone,

First of all, this is strictly for Windows platforms and preferably using ActiveState Perl 5.8.8.

I'm trying to figure out how I can take a file that is currently opened in another program such as Microsoft Word, Paint, etc. and slurp the file data into a Perl variable?

Also, I'm not trying to write back to the opened file or manipulate it in any other way than Reading its data. However, at some point I need to determine whether the data is Binary or Ascii but not sure if I should perform this before the slurp or after?

Replies are listed 'Best First'.
Re: File Saving
by BrowserUk (Patriarch) on Nov 08, 2008 at 16:07 UTC
    I'm trying to figure out how I can take a file that is currently opened in another program such as Microsoft Word, Paint, etc. and slurp the file data into a Perl variable?

    The answer to this will depend upon how the other application has opened the file, and will vary from 'trivial' to 'impossible'.

    • If the other process hasn't specified DENY_READ, then you should be able to open it for reading and slurp it in any of the usual Perl ways.
    • If the other process has specified DENY_READ, then you are out of luck.

    Of course, the user id that your program runs under will need to have read permissions for the file and directory it is in, regardless of the above.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks for the reply.

      Do processes typically lock files during runtime or is this an exception and not the rule?

        It absolutely depends upon the application. My editor does. Typically spreadsheets do.

        As a rule of thumb, if the application is modifying the file in-place, then it will deny read access, as any asynchronous read might read a non-coherant intermediate state.

        That said, I've got three different video player apps, and even on the same filetype one denies all access, one denies write access and the third doesn't appear to care what other apps do to the file after it has opened it.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: File Saving
by ig (Vicar) on Nov 08, 2008 at 18:08 UTC

    Even if files are locked against reading you should be able to access them if you are on a recent version of Windows that supports the volume shadow copy service. You will have to create a volume shadow copy and read the file from there.

    In any case, if the application has the file open at the time there is no guarantee that the data on disk will be in a consistent state. If you know the application that has the file open (e.g. Word, Excel, etc.) it may be better to take control of the application to save a copy. For this you would need to use the COM interface of the application. There are modules to support some of the more common applications or you can use COM directly, if necessary.

      Again, Thank you, I appreciate the advice. I thought of using COM, however if an application freezes and isn't responding then not sure if COM is the answer. I may still give it a try anyway.
Re: File Saving
by Mr. Muskrat (Canon) on Nov 09, 2008 at 16:39 UTC
    Your original post didn't mention anything about trying to write a crash recovery script but your last reply implies it. Instead of telling us one of the steps that you are trying to accomplish and filling some of the details as you go, why don't you just explain the thing in full.
      Sorry, I apologize for my vagueness, but as I'm stuck having to write this script for work :( I'm not always allowed to divulge all the information I would like to.

      But to answer your question, it is a crash recovery script that acts on a set of known, open files and saves them at the event of a crash or application lockup.

      I was hoping that there was a relatively painless way of doing that but now I'm not so sure.