Monday, May 24, 2010

Perl Script To Delete Dynamically Created Files?

I need a Perl script to help clean up dynamically created files. The files are in the format ABC###.gif Where ### is some randomly created alphanumeric character. The part I'm having trouble is some of these files will be in use by web users. How will I know this, or how can I make sure I'm not deleting an in use file?

Perl Script To Delete Dynamically Created Files?
What do you mean that you are concerned that it will be in use by users? If just viewed on a page, the image won't disappear from the users page if you delete it. If the user is actually doing something to the actual file, then change the script that allows the user to manipulate the file. Create a temp file for the user to manipulate then write the completed file to your directory that you want to delete from, then if you happen to delete a file that is being worked on, it doesn't matter.





For the deletion:


my @filesToDelete = glob("/whateverdir/ABC*.gif");


foreach (@filesToDelete){


unlink $_;


}
Reply:I'm inclined to think that you will not no if it is in use unless that web user has an open file handle on the file, in that case the file would not be able to be deleted. Given the nature of the web I'm betting that there is no open file handle. This would mean that unless there is a mechanism by which the web site/application keeps track of the the in use files there is not a practical way for you to know if it is being used.





You might be able to use the atime of the file to help deduce whether or not the file was used today. Basically the answer to your question is that there either needs to be a rule that determines the retention period of these files or the web site will need to assist you in knowing what files are still being referenced.


No comments:

Post a Comment