Solved

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

Posted on 2014-10-23
12
48 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to extract data from a table into a fixed length file 10 54
HTTP Error 502.2 - Bad Gateway 3 214
PERL variable conundrum 9 90
Call Shell Script from Perl Script 6 97
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

813 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