LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   perl- check size to see if file is complete (http://www.linuxquestions.org/questions/programming-9/perl-check-size-to-see-if-file-is-complete-620375/)

michael.barnes 02-11-2008 02:50 PM

perl- check size to see if file is complete
 
I'm trying to use perl to manipulate some files in a drop box environment. Everything is fine until someone drops a big (300+MB) file in the dropbox folder. It seems the script attempts to do its thing before the file is entirely copied into the dropbox. I was thinking of checking the file size every few seconds and when it came back three times the same size it could be assumed to be complete and press on. I've been trying to sort out lstat to do this but I can't seem to get my loops set up right. Basically, I want to do this:

glob the folder to an array
foreach each file
file size = $size
wait a few seconds
does file = $size?
repeat until file = $size three times

then continue processing file
go back to array grab next file and do it again.

Is my approach valid?
Any suggestions of perl script segments for each part of this game?

Thanks for helping a noob.

gilead 02-11-2008 04:17 PM

Do you have any control over the application dropping the files into the directory? We set up a process on a project here where each file being dropped had a zero byte flag file with it.

Basically what happened was this:
- Application writes data file (*.dat) to directory;
- After data file is written, application writes 0 byte flag file (*.flag) to directory.

The application polling the directory looks for the flag files. It's process is simple:
- Poll for flag files;
- Process the data file for each found flag file;
- Delete the data and flag file.

All of which is not much use if you can't control the application dropping the files into the directory...

chrism01 02-13-2008 06:20 PM

Gilead's soln is good, used it myself in the past.
Otherwise a simple loop ?
eg
Code:

$cnt=0;
$last_size=0;
$curr_size=0;

for $file ( @file_list )
{
    $last_size = (stat($file))[7];
    while(1)
    {
        sleep(5);
        $curr_size = (stat($file))[7];
        if( $curr_size == $last_size )
        {
            $cnt++;
            if( $cnt >= 3 )
            {
                action_file($file);
                last;
            }
        }
        $last_size = $curr_size;
    }
    $last_size = 0;
    $curr_size = 0;
    $cnt = 0;
}

NB: Untested (!) but you get the idea ...


All times are GMT -5. The time now is 12:31 PM.