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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

First script in Perl

Status
Not open for further replies.

jpillonel

Programmer
Dec 17, 2003
61
CH
Hello,

This is my first script in Perl. I have to clear a folder on Windows 2003 server for files (logfiles) older than 14 days. The remove could be based on the system timestamp of the file of on the filename because the filename like:
DailyMpgateway.log.2005-09-15

For that I do this little just to print the files but I don't know how to glob files only for the last 14 days:

#!C:/Perl/bin/perl.exe -w
use Win32;
# Print the directoy where the perl script start;
$currpath = Win32::GetCwd();
print "Cuurent directory is $currpath \n";

# Changing directory and printing the current folder
chdir("c:/dev/perl/LOG") || die "cannot cd to C:\dev\perl\LOG ($!)";
$currpath = Win32::GetCwd();
print "Successfully changed to folder: $currpath \n \n \n";

@all_logs = glob("DailyMpgateway.log.*");

print "List of Logs present on folder: \n";

foreach (@all_logs) {
next unless -f;
next unless -r;
$age = -M;
print "Log: $_ Date: $age \n";
}

Thanks for your help !
 
maybe more like this, use grep instead of glob:

Code:
use Win32;
# Print the directoy where the perl script start;
my $currpath = Win32::GetCwd();
print "Current directory is $currpath \n";

# Changing directory and printing the current folder
chdir('c:/dev/perl/LOG') or die 'cannot cd to C:\dev\perl\LOG' . "($!)";
$currpath = Win32::GetCwd();
print "Successfully changed to folder: $currpath \n \n \n";
opendir(DIR,'.') or die "$!";
my @all_logs = grep(/^DailyMpgateway\.log\./ && int -M > 14, readdir DIR);
close(DIR);
print "Files to be deleted:\n";
print "$_\n" for @all_logs;
#uncomment next line only when ready to actually delete files!!!!
#unlink for @all_logs;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top