Solved

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

Posted on 2014-10-23
12
51 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

861 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