Click to See Complete Forum and Search --> : Help with Perl Unlink
MrNewbie
07-30-2001, 09:52 AM
I need to delete all files older than a certian file like 12Jul2001.html and everything before that, that means 11Jul2001.html, 10Jul2001.html, 15May2001.html etc, I know you can use wildcards in the unlink command but I just can't figure out how to do this. Its easy to delete the file from exactly one year before but I want to delete all the files before then too, can anyone help me with a way to do it?
Thanks
EyesWideOpen
07-30-2001, 10:31 AM
If you were to build a list of the files you want to delete then you could call unlink on the list as:
unlink @goners;
A syntax for using wildcards with the unlink command would be (got this from Programming Perl 3rd Ed.):
unlink glob("*.html");
[ 30 July 2001: Message edited by: EyesWideOpen ]
TheLinuxDuck
07-30-2001, 11:02 AM
MrNewbie:
I don't think that you're going to be able to delete all files of successive years/dates with only one unlink command, unless the files are in an array (like EWO suggested), or with a list of scalar names.
I mean, I don't think that a wildcarded name will work for all files of and before a specific date.
You'll prolly have to do a date parse that checks the directory for the earliest date, and then determines what date to delete from, and then from there, loop through each date (or each month, using wildcards, depending on how the files are named)
YaRness
07-30-2001, 11:14 AM
me too!
err, i mean, i second tld's notion. parse out the date, pick all the files with <= date you want to start deleting from, put the filenames in a list, and unlink @list.
i bet if you check cpan, or modules that come with perl, that you will find one that will compare dates for you. less stuff to code.
MrNewbie
07-30-2001, 01:35 PM
Thanks, when I get the files I'll put the names in an array and unlink them from there. But when you say date do you mean the date as in the file name or the file creation date? Because wouldn't it be easier to find the file creation date?
If you mean the creation date I think I found a test.
Something like.
$yearoldfile = -M "29Jul2001.html";
(Btw searching through a file won't consider it modified will it?)
while(there's another .html file){
if(-M "thecurrentfile.html" > $yearoldfile){
unlink("thecurrentfile.html");
}
}
[ 30 July 2001: Message edited by: MrNewbie ]