EyeOpener has asked for the wisdom of the Perl Monks concerning the following question:
I've got a script which monitors a directory on an AIX system. If a new file is added, it opens the file, munges it around, and moves the original. Here is what the meaty part of the script looks like:
while (1) { my @files = glob "*"; foreach my $file (@files) { dostuff($file); } } sub dostuff { my $victim = shift; open IN, "$victim" or die "Can't open input file $victim: $!\n"; # more stuff here close IN; rename "$victim", "/z/saved/$victim"; }
The problem is that the filename is grabbed before the file is completely written. If somewhat large files are FTP'd into this directory, the script tries to open the file while the FTP is in progress, and the script dies on the open IN line with the error "Cannot open or remove a file containing a running program."
Is there a file test I can use to determine whether the file is still being written, or is open by another process? Alternatively, it would suffice to trap the error and retry until the file is ready. I'm not really familiar with eval, and I can't find a good example of this kind of loop. Can anyone suggest a good approach? Thanks!
Peter
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Determine when file is done being written?
by Weasel (Sexton) on Jun 12, 2002 at 18:18 UTC | |
by EyeOpener (Scribe) on Jun 12, 2002 at 20:24 UTC | |
by Weasel (Sexton) on Jun 12, 2002 at 20:50 UTC | |
Re: Determine when file is done being written?
by jwest (Friar) on Jun 12, 2002 at 18:44 UTC | |
Re: Determine when file is done being written?
by how do i know if the string is regular expression (Initiate) on Jun 12, 2002 at 18:43 UTC | |
Re: Determine when file is done being written?
by cmilfo (Hermit) on Jun 12, 2002 at 19:39 UTC | |
Re: Determine when file is done being written?
by Anonymous Monk on Jun 12, 2002 at 19:00 UTC |