Solved

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

Posted on 2014-10-23
12
29 Views
Last Modified: 2016-06-07
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;
    }
0
Comment
Question by:JJSystems
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40400731
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.
0
 

Author Comment

by:JJSystems
ID: 40404815
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.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40404897
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".
0
 

Author Comment

by:JJSystems
ID: 40404979
It's third party software depositing the file.  That's not one of my options.
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40405121
If you can't modify how the file is copied to the system, there really is no good option.  You can't modify the third party software or have the vendor do so?

The only other suggestion I have is to figure out the longest it takes a file to be fully copied and then double it.  Have the script wait that long after it first sees the file before doing anything with it (and assume it is stable at that point).

The final option I can think of is to switch the system the files are copied to to a different OS.  It's not a good option but it would likely fix your "stable" problem.
0
 

Author Comment

by:JJSystems
ID: 40405132
Thanks anyway.  I have that in place now as a work around, but I need to find a better solution.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Expert Comment

by:ozo
ID: 40407781
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?
0
 

Author Comment

by:JJSystems
ID: 40408144
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40408151
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?
0
 

Author Comment

by:JJSystems
ID: 40408178
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40408190
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?
0
 
LVL 9

Expert Comment

by:Suhas .
ID: 41640951
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now