Closed Thread Icon

Topic awaiting preservation: Leaving Off Directory in Output (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=23527" title="Pages that link to Topic awaiting preservation: Leaving Off Directory in Output (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Leaving Off Directory in Output <span class="small">(Page 1 of 1)</span>\

 
Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 10-03-2004 23:31

With what little knowledge I have of Perl syntax combined with a wee bit of cannibalism, I hacked together this script that will delete images older than X number of days from a certain directory and log the results ...

code:
#!/usr/bin/perl -l

$Directory = "/home/user/directory/files";
$LogFile = "/home/user/directory/cleanup.log";
$Days = 7;

$Seconds = $Days * 86400;
$Date = localtime (time);
$Deleted = 0;
$Skipped = 0;


# Double >> will append file if it exists. Single > overwrites.
open(OUTPUTFILE, ">>$LogFile") || die "open0: $!";

print "\n==============================";
print "Checking files in directory \"$Directory\".";
print "Files older than $Days days will be deleted.";
print "----------------------\n";

print OUTPUTFILE "\n========== Open Log ($Date) ==========\n";
print OUTPUTFILE "Checked files in directory \"$Directory\".";
print OUTPUTFILE "Files older than $Days days were deleted.";
print OUTPUTFILE "----------------------\n";

for (glob("$Directory/*")) {
if (-f && (time - (stat($_))[9] >= $Seconds)) {
unlink;
print " DELETING: $_";
print OUTPUTFILE " DELETED: $_";
$Deleted++;
} else {
print " Skipping: $_";
print OUTPUTFILE " Skipped: $_";
$Skipped++;
}
}

if ($Deleted < 1) {
print "\n NO FILES FOUND OLDER THAN $Days DAYS";
print OUTPUTFILE "\n NO FILES FOUND OLDER THAN $Days DAYS";
}

print "\n----------------------";
print "$Deleted files were deleted.";
print "$Skipped files were skipped.";
print "==============================\n";

print OUTPUTFILE "\n----------------------";
print OUTPUTFILE "$Deleted files were deleted.";
print OUTPUTFILE "$Skipped files were skipped.\n";
print OUTPUTFILE "======================= Close Log =======================\n\n";
close(OUTPUTFILE) || die "close0: $!";



I have only one annoyance: Because I'm running this as a cron job, I have to specify my entire user directory path, which is actually much longer then the dummy one I put up there. Thus, the entire directory path gets output in the log, like so:

quote:
========== Open Log (Sun Oct 3 14:11:15 2004) ==========

Checked files in directory "/home/user/directory/files".
Files older than 7 days were deleted.
----------------------

DELETED: /home/user/directory/files/old.txt
Skipped: /home/user/directory/files/young.txt
DELETED: /home/user/directory/files/older.txt

----------------------
2 files were deleted.
1 files were skipped.

======================= Close Log =======================



Now, I wouldn't mind keeping the entire directory path in the "Checked files in directory X" statement, but is there any way I can list only the file names in the DELETED/Skipped list?

By the way, for anyone wishing to use this script, you can test it by creating old/new files with the following commands:

quote:
touch -m 01020001 old.txt (Creates a file with the modified date of Jan. 2, 12:01 a.m.)
touch -m 01010001 older.txt (Creates a file with the modified date of Jan. 1, 12:01 a.m.)
touch -m new.txt (Creates a file with the current date/time. Yeah, there's probably a more appropriate way to do this.)



bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-04-2004 01:00

Just use File::Basename and then when you print you can just

print OUTPUTFILE " DELETED: ".basename($_);



.:[ Never resist a perfect moment ]:.

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 10-04-2004 01:25

Ah, worked perfectly.

Thanks!


BTW, for anyone reading this who wasn't lucky enough like me to see an entry like this earlier today, you have to include the following in the script for the above to work:

code:
use File::Basename;



« BackwardsOnwards »

Show Forum Drop Down Menu