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
In reply to Determine when file is done being written? by EyeOpener
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |