http://qs321.pair.com?node_id=103986


in reply to Re: Re: Re: Aging Script
in thread Aging Script

The only thing changed in the new code is the -h to -d. I still have to replace the path names..to find and delete the files on my PC.
Here is the new code:
#!/usr/local/bin/perl -w #Call Module use File::Find; use File::Copy; use Getopt::Std; getopts('d:h'); #Initialize Variables $server='server104'; $dirl='\\\\'."$server".'\\d$'; $agedays=60; #defaults agedays to 60 #Process arguments ([h]elp, [d]ays) if ($opt_d){ die "\agecmd.pl -d[days to age]\n"; } if ($opt_d){ if ($opt_d gt 9){ #tests for strings die "Error: days option must be a number. Use agecmd.pl -d[da +ys to age]\n"; } else{ $agedays=$opt_d; } } #Do date calculations @xtime=localtime(time); $day=$xtime[3]+1; $month=$xtime[4]+1; $year=$xtime[5]+1900; $hours=$xtime[2]; $mins=$xtime[1]; $secs=$xtime[0]; $logname='//server1/d$/'."ftp$month$day$year$hours$mins$secs".'.log'; $agesecs=60*60*24*$agedays; #converts $agedays to seconds $daysago=time-$agesecs; #the time stamp of $agedays ago in seco +nds $daysago2=localtime($daysago); #the time stamp of $agedays ago in w +ords - mainly for printing #Find the files (no dir) on the server print "finding files to be aged\.\.\.\n"; find(\$wanted, $dirl); sub wanted { $filesecs = (stat("$File::Find::dir/$_"))[9]; #Gets the 9th ele +ment of file stat - + # the modified time $filesecs2=localtime($filesecs); if ($filesecs<$daysago && -f){ #-f=regular files, eliminate + DIR p.367 push (@files,"$File::Find::dir/$_"); push (@files,"$filesecs2"); } print'.'; } #replace '/' with '\' in file names foreach (@files){ s/\//\\/g; } #Write to Log %filehash=@files; #puts the array into a hash for easy printing. Ke +y=file, Value=date open OUT, ">$logname" or die "Cannot open $OUT for write :$!"; print OUT "FTP server aging log generated by PERL script\n"; print OUT "Script written by Ozzy on 4/18/00\n"; print OUT "Files deleted from \\\\$server on ".localtime(time)."\n"; print OUT "Files deleted before $daysago2\n"; print OUT "Files were aged $agedays days\n\n"; print OUT "File Listing:\n\n"; foreach $filename (sort keys %filehash){ print OUT "filename $filehash{$filename}\n"; } close OUT; #Delete files print "\nDeleting all files before $daysago2"; #unlink $filehash; print "\nScript complete.";
~Seema

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Aging Script
by OzzyOsbourne (Chaplain) on Aug 10, 2001 at 22:34 UTC

    With this code, you are looking for the d option twice. If you use the -d option (agecmd.pl -d90) at all, the code will die. Are you running the code with the d option? If not, you don't really need the whole getopt section. Do you have the Getopt::Std module installed?

    -OzzyOsbourne

      I was aware that I was looking at the -d option twice. Though when I ran the script that you had orginally gave me, I got an error stating: that -h option was only used once. I think thats why I changed it..and now I am confused.
      I want to use the -d option..but don't know how to go around the error I am getting. No, I dont' have Getopt::Std module installed. I am very new to cron..so if you don't mind explaining that bit...that would be great. Thanx!
      ~Seema

        Option 1:In order to use the otions in the script, you have to have getopt::Std installed (use Getopt::Std) as mentioned at the top of the script. Read here for more info. This should solve your issue.

        Option 2:Wipe out all of the options, and setting $agedays=$ARGV[0] which would be the same as running agecmd.pl days. This requires no additional modules, but you'd have to rewrite the error checking so that the argument is a number and not "xfl" or something equally incorrect. Here is a thread referencing this option.

        Option 3: Or you can wipe out the whole options section at the top and just simply set $agedays to whatever number of days you require.

        -OzzyOsbourne