in reply to File::Copy - move() function corrupting files

If I understand correctly, you have problem when your program starts processing files that are not completely there (still being copied from somewhere else), or whatever. You need to prevent your program from grabbing your PDF file prematurely.

There are several things you could do: when you detect a new PDF, just sleep for some time before starting to process it (that works only is the time for the file to arrive is relatively constant). Another way is to have the process delivering the files to copy the files with a different name (e.g. an extension other that ".pdf", such as ".tmp"), and to rename the file only when the file delivery is complete. Or the process delivering the files could put a flag (an empty file with a name similar to the file being delivered) in the directory and remove the flag once delivery is complete. This way, your program knows when it can start processing the file safely

  • Comment on Re: File::Copy - move() function corrupting files

Replies are listed 'Best First'.
Re^2: File::Copy - move() function corrupting files
by myelinviolin (Novice) on Aug 25, 2014 at 16:19 UTC

    The only way I could get my program to work is to wait 5 seconds after it detects a file. I tried really hard to just get it to move the oldest file with File::DirList

     @list = File::DirList::list('$directory','ia',1,1,1);

    and stat, but I could never get the number out of the variables.

    $last_modified = (stat($filename))[9]

    My program works for me for now but who knows the kind of pressure people are going to put on it. Any hints on these in case I really do need to upload the oldest file?

    My new code:

    use CAM::PDF; use File::Copy; use File::stat; use Time::localtime; sleep(1); if ($o==0){ print "\nChecking for files...\n"; $o=1; } #Initialize title page pdf my $doc1 = CAM::PDF->new("$file1") || die "$CAM::PDF::errstr\n"; #Read each pdf file on the desktop opendir(DIR,$directory); my @files = grep{/\.pdf$/}readdir(DIR); if (@files){ print "Found file! Waiting 5 seconds before I move it.\n"; print "Name of file: $files[0]\n"; $o=0; sleep(5); } else{ continue; } closedir(DIR);
      Well, do you have control on the process producing the files onto the desktop? Because the best solution is really to change this process so that it copies the files with a different name (say "*.pd_"), and that it renames them to "*.pdf" only when the file is complete. This way, when you grep the directory content, you only pick up files that are complete.

      Or create the files in a different directory on the same disk, and move it into the right directory once the file is complete.