Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
    0: # Deletes all files older than the number if days input
    1: # Written by Jonathan E. Dyer 4/18/00
    2: # Server varibale is hard coded and named $dir1, b/c this script can be dangerous
    3: 
    4: # *********************************
    5: # Call Modules
    6: # *********************************
    7: 
    8: use File::Find;
    9: use File::Copy;
    10: use Getopt::Std;
    11: getopts('d:h');
    12: 
    13: # *********************************
    14: # Initialize variables
    15: # *********************************
    16: 
    17: $server='server1';
    18: $dir1='\\\\'."$server".'\\e$\\clients';
    19: $out="c:/$server.txt";
    20: $agedays=60; #defaults agedays to 60
    21: 
    22: # *********************************
    23: # Process arguments ([h]elp,[d]ays)
    24: # *********************************
    25: 
    26: if ($opt_h){
    27: 	die "\agecmd.pl -d[days to age]\n";
    28: }
    29: 
    30: if ($opt_d){
    31: 	if ($opt_d gt 9){ #tests for strings
    32: 		die "Error: days option must be a number.  Use agecmd.pl -d[days to age]\n";
    33: 	}else{
    34: 	$agedays=$opt_d;
    35: 	}
    36: }
    37: 
    38: # *********************************
    39: # Do date calculations
    40: # *********************************
    41: 
    42: @xtime=localtime(time);
    43: $day=$xtime[3]+1;
    44: $month=$xtime[4]+1;
    45: $year=$xtime[5]+1900;
    46: $hours=$xtime[2];
    47: $mins=$xtime[1];
    48: $secs=$xtime[0];
    49: $logname='//mfa3e04/e$/'."ftp$month$day$year$hours$mins$secs".'.log';
    50: 
    51: $agesecs=60*60*24*$agedays;	#converts $agedays to seconds
    52: $daysago=time-$agesecs;		#the time stamp of $agedays ago in seconds
    53: $daysago2=localtime($daysago);	#the time stamp of $agedays ago in words - mainly for printing
    54: 
    55: 
    56: # *********************************
    57: # Find the files (no dir) on the server
    58: # *********************************
    59: 
    60: print "finding files to be aged\.\.\.\n";
    61: find(\&wanted, $dir1);
    62: 
    63: sub wanted {
    64: 	$filesecs = (stat("$File::Find::dir/$_"))[9]; #GETS THE 9TH ELEMENT OF file STAT - THE MODIFIED TIME
    65: 	$filesecs2=localtime($filesecs);
    66: 	if ($filesecs<$daysago && -f){ #-f=regular files, eliminates DIR p.367 
    67: 		push (@files,"$File::Find::dir/$_");
    68: 		push (@files,"$filesecs2");
    69: 	}
    70: 	print'.';
    71: }
    72: 
    73: # *********************************
    74: # replace '/' with '\' in file names
    75: # *********************************
    76: 
    77: foreach (@files){
    78: 	s/\//\\/g;
    79: }
    80: 
    81: # *********************************
    82: # Write to Log
    83: # *********************************
    84: 
    85: %filehash=@files; #puts the array into a hash for easy printing.  Key=file, Value=date
    86: open OUT, ">$logname" or die "Cannot open $out for write :$!";
    87: print OUT "FTP server aging log generated by PERL script\n";
    88: print OUT "Script written by Jonathan E. Dyer on 4/18/00\n";
    89: print OUT "Files deleted from \\\\$server on ".localtime(time)."\n";
    90: print OUT "Files deleted before $daysago2\n";
    91: print OUT "Files were aged $agedays days\n\n";
    92: print OUT "File Listing:\n\n";
    93: foreach $filename (sort keys %filehash){
    94: 	print OUT "$filename	$filehash{$filename}\n";
    95: }
    96: close OUT;
    97: #!/usr/bin/perl -w
    98: 
    99: # *********************************
    100: # Delete files
    101: # *********************************
    102: 
    103: print "\nDeleting all files before $daysago2";
    104: unlink %filehash;
    105: print "\nScript complete.";
    

In reply to Find files older than x days and delete them by OzzyOsbourne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found