How to check if moving a file is finished in perl?

591 views Asked by At

I have a Perl script that moves files into folders. Every time it runs it checks the filenames in the folders. But if I have a bigger file it might not be ready with the moving when it checks the filenames, and it really can mess up stuff. I would like to stop at the end of the loop and check that the moving is finished, before it continues.

2

There are 2 answers

1
Dave Cross On BEST ANSWER

Three common approaches.

  1. Monitor the file size. Assume the file is complete when the size hasn't changed for X seconds. Non-trivial to implement and prone to errors.
  2. Upload a marker file after the moved file is complete. Check for the existence of the marker rather than the original file.
  3. Copy the file using a different name. Rename it once the transfer is complete.
2
AudioBubble On
sleep 1 while -e $original_filename;

There are numerous options and even an infinite while option, but you need to format the code in order to ensure you do not put the script in a permanent loop if the file will never exist.