scopey has asked for the
wisdom of the Perl Monks concerning the following question:

Due to hdd space constraints, I'd like to write a script that will automatically move freshly generated output files to another disk with lots of free space. This script will run on a 2nd PC. All PCs are Windows. So far, the best idea I could think of involves checking file attributes, namely the size element of stat, and keeping track of its state so that I know when the file has stopped growing and therefore it's time to move it. Like this:

Under Linux you can use the fuser command to determine if another process has the file open. That would be another useful piece of information to use in your determination of whether or not the log file is still being written to.

I'm sure there is something equivalent for Windows. You can also use the fact that some Windows commands don't let you rename files which are open or rename directories which have open files under them them. Don't know what they are, but I run into that problem all the time using the Windows Explorer.

The problem is a bit under-specified. The cycle to test a resource (read value, sleep, read value, sleep, etc.) is correct. Well, assuming that your print inside foreach is just a placeholder and you actually do some processing there such as getting the size and doing something with the file if size ... (your criteria here).

What is not clear is what you mean by "the file has stopped growing". I hope you don't conclude "hard disk full" when this happens... The semantics and dynamics of log file access are not known in your problem description. Based on what I read, the only thing that can be improved it fine-tuning the sleep interval ("stopped growing" means two consecutive reads yield the same size? or size doesn't change in 5 minutes? it is not clear ...).
If you want the file to be moved when it reaches a certain size (popular option in handling logs), you can fine-tune your sleep based on the file growth speed (oh, well, not always easy to guesstimate, depending on the use scenario ...) and just move the file the first time it exceeds your size limit (that would be checked for in the foreach body.

Hard to give more details/opinions with only the info you provided, but your approach is good.

unless this is for theoretical purposes only as an excercise in perl....data is normally moved off to separate storage in chunks (such as entire directory trees) with due regard given to access to that data from the application(s) that need it. It doesn't make sense to continue directory contents onto another server with such manual (and easily breakable) code.
you may be able to leverage MS DFS technology for unifying UNC access or other filesystem/clustering technology

I suspect you want to wait until the writing process closes the file before you copy it and are using the fact that the file isn't growing anymore to assume this has occurred. Since this is Windows, you could probably take advantage of file locking instead. If you attempt to open the file without allowing other accessors, the open should fail unless the other process has closed it. You could retry with a delay until successful.

Another option might be to use a utility like RoboCopy which should be available from one of Microsoft's download sites if it's not on your system already. It should detect locked files and exit with a status indicating whether it succeeded or not.

Thanks to everyone for all the thoughtful replies, and taking time to sort out my somewhat lackluster description of the task. :)
I tried several methods, including attempting to open the file for writing, but found what seems to be a fairly simple (and most reliable) solution using Perl's 'move' command. The following script can be run as is, provided the user sets the proper input/output directories and desired filetype. On my PC, this script runs with no noticeable effect on CPU usage. Any comments appreciated.

When putting a smiley right before a closing parenthesis, do you:

Use two parentheses: (Like this: :) )
Use one parenthesis: (Like this: :)
Reverse direction of the smiley: (Like this: (: )
Use angle/square brackets instead of parentheses
Use C-style commenting to set the smiley off from the closing parenthesis
Make the smiley a dunce: (:>
I disapprove of emoticons
Other