Wiggins D Anconia wrote: > > > Neill Taylor wrote: > > > > > > I am writing a script which will poll a directory and then move any > files > > > in the directory to a new directory dependant upon its name. > > > > > > The problem I think I might have is that the script may try to pick > a file > > > that is still being copied into the directory whilst the application is > > > still trying to write the file. > > > > > > What is the best method in Perl to prevent errors. > > > > Hi Neill. > > > > If your application is well-behaved then it may lock the file > > for writing. If your Perl program asks for an exclusive lock > > then it will be granted if it no longer being accessed. > > Take a look at > > > > perldoc -f flock > > > > Another way is to check the size of file. A file will often > > show a size of zero until it is closed by the writing > > application. > > > > Finally, if only one file at a time is being written then > > just sort the them in order of creation date and move all > > but the latest one. > > > > What works you will find only by experimentation. > > > > Depending on what is writing the files, aka another controllable > program? I have had good luck with writing the file to a temporary > location, usually with a dot on the front, then executing a move from > that temp location to the real name. A move is usually a single action > (at least that's my understanding) and for most filesystems is merely a > single inode update. Then just have the directory watcher skip dot > files... Though the lock would probably be better...
Hi Wiggins. Yes, that will often work, depending on the filing system. But a 'move' will usually do 'copy' followed by 'delete'. Occasionally you can move a file while it is being written to, as the application doing the writing doesn't care where it appears in the directory structure. But that's unusual. It's more common that the 'copy' will be allowed to grab an image of the data flushed so far and the subsequent 'delete' will be executed once the writing app has closed the original file. Without extensive knowledge of the platform and filing system I don't think it's safe to assume anything! Cheers, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>