Link to home
Start Free TrialLog in
Avatar of JJSystems
JJSystems

asked on

How to tell if a file is being written with Perl running on a Windows 2003 Server

I had a Perl Script that used to run on a Mac platform with no problem.  When I run it on Windows, it always reports the file is stable.  The subroutine below would verify that the file size is the same as it was 10 seconds ago, and consider the file stable.  How can I get this working in Windows?

sub stable
   {
      $solid = "no";
      $sizeof = (stat($File))[7];
      sleep(10);
      $sizeof2 = (stat($File))[7];

      if ($sizeof == $sizeof2) {
        $solid = "yes";
        }
      else {
        $solid = "no";
      }
      return $solid;
    }
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

I'm not sure why your code would not work.  I'd first try changing to "$sizeof[2] = -s $File" and see if that works.
Avatar of JJSystems
JJSystems

ASKER

Windows reports the size of the source file immediately, instead of gradually increasing the file size as the file copies.  I'm stumped with this one.
Ah.  I missed that you were copying a file.  I'm pretty sure there are other OS's that do the same thing.

What you need to do is one of:
Copy the file to a temp name (common is to prepend ~ or # or append .tmp) and then rename it to the correct name after the copy.  Then, if the real file exists, it is stable (because the copy was done before the rename).  If you are copying via ftp, some ftp software (such as ncftp) includes this functionality built-in.
Copy a dummy file (named something like <filename>.ready) after copying the real file.  Then check if the .ready file exists rather than checking if the main file is "stable".
It's third party software depositing the file.  That's not one of my options.
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks anyway.  I have that in place now as a work around, but I need to find a better solution.
What happens if you try to read the file when the size seems stable but the file is still being written?
Can you detect whether the third party software is still running?
If I try to move it while it is being written, I see an error on the console that the file is in use by another process.
Then would it work to have the perl script try to move it every 10 seconds, and when it succeeds without error it can then tell that it is not being written?
I've got it doing that now (actually 20 seconds) but there are all of those annoying console messages.  
I though for sure there would be a better way.
How is it doing the move?
Can you redirect the console messages?  
Are they on stderr?

If a move generates an error, what does a -w test or a (stat)[3] on the file report?

Rather than a move, can you do a copy or a link?
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: wilcoxon (http:#a40405121)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

suhasbharadwaj
Experts-Exchange Cleanup Volunteer