-2

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.

TheAdam122
  • 99
  • 11
  • 2
    Post your code. –  Mar 27 '17 at 07:40
  • you could probably check whether the file exists in the new location after the loop. – Mohit Mar 27 '17 at 07:49
  • It's too vague to give you an answer. What you need is some sort of lock mechanism, like `flock` - – Sobrique Mar 27 '17 at 08:33
  • Is the problem that you are seeing a partial file? If so, move using `rename` since it happens atomically. If you can't use `rename`, `move` the file to a temporary directory from which you can `rename`, then use `rename`. – ikegami Mar 27 '17 at 17:24

2 Answers2

1

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.
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Yes. Move the file to `your_file_name.in_progress`. When the move has finished, rename it to `your_file_name`. Note that moves as long as you're on the same file system are usually pretty much instantaneous - as it's just updating the directory table rather than actually moving data around. – Dave Cross Mar 27 '17 at 09:27
0
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.