Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete files/directories by name under NT + Unix

Status
Not open for further replies.

andimue

Programmer
Jun 29, 2001
18
DE
Here is what I am trying to do : I got software that runs under Sun Solaris + Windows NT. This software produces log-files and also log-directories, which are named for example
msg20010925 ( msg + year + month + day ). I am looking for a perl-script to "purge" away this files, specifying how much files/dir to keep. So far I already have a script thats working fine. Now I got a new version of my software. This guys changed the name of some log-files to msg20012509 (msg + year + day + month ) so my script doesnt work any longer, because I sorted the files by name and not by date:

open(DIRLIST,$dircmd) or die "Error ! \n" ;
$found = @filelist = (sort {$b cmp $a} <DIRLIST>);

I would a appreciate every suggestion how to solve this problem.

Andi :)
 
In order to do this sort, you need to split up the data into year, month, and day. Here is one way to do it.
Code:
open(DIRLIST,$dircmd) or die &quot;Error ! \n&quot;  ;
sub mysort {
    ($yrA, $dayA, $monA) = $a =~ m/^\w{3}(\d{4})(\d{2})(\d{2})/;
    ($yrB, $dayB, $monB) = $b =~ m/^\w{3}(\d{4})(\d{2})(\d{2})/;
    $yrA <=> $yrB || $monA <=> $monB || $dayA <=> $dayB;
}
$found = @filelist = (sort mysort <DIRLIST>);
 
First of all: Thanks raider2001 !
I got this.
Let me ask one more thing : I wanna make the script more variable, so that I can handle files with date and/or time at different positions in their names, for example :
20011231_071358.log or logfile_20013112.txt

I thought about giving the script the filename and the position, where the date/time is located in the filename.
But I tryind and failed :-(

Maybe you or someone else can help me once more ?

andi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top