Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Aging Script

by OzzyOsbourne (Chaplain)
on Aug 08, 2001 at 22:37 UTC ( #103161=note: print w/replies, xml ) Need Help??


in reply to Aging Script

I did something like this a while ago. Because I did't know about strict at the time, you will have to modify it slightly...Example

-OzzyOsbourne

Replies are listed 'Best First'.
Re: Re: Aging Script
by Seema (Novice) on Aug 09, 2001 at 01:36 UTC
    I ran the script again. I am getting an error in the 'Process arguments(help, days).
    It says: Name "main::opt_h" used only once.
    For:
    if($opt_h){ die "\agecmd.pl -d[days to age]\n"; } if($opt_d){ if($opt_d gt 9){ die "Error: days option must be a number. Use agecmd.pl -d[days to a +ge]\n"; } ...
    Should the code be???
    if($opt_h){ die "\agecmd.pl -d[days to age]\n"; } if($opt_d){ if($opt_d gt 9){ die "Error: days option must be a number. Use agecmd.pl -d[days to a +ge]\n"; } ...
    ~Seema
Re: Re: Aging Script
by Seema (Novice) on Aug 09, 2001 at 01:17 UTC
    Thanx Ozzy...
    I checked out the script you wrote. I have taken a few things out...though the script runs just fine! Thanx again!
    ~Seema
Re: Re: Aging Script
by Seema (Novice) on Aug 09, 2001 at 23:44 UTC
    Hi Ozzy..
    I tried the example that you had given to me yesturday. I did have an error for the opt_h and opt_d.
    I replaced the opt_h command with the opt_d command, now the script runs just fine. Though I know this is not the way you have made the script. I know the way I have changed it, is wrong. Could you please let me know what I can do!?! I know I need the opt_h because its the help command and opt_d is the days command. Please let me know as soon as you can. Thanx for your help!
    ~Seema

      the 1st if statement here says if the user uses -h appended to the end of the script file, die an tell them how to run the command properly.

      the second part says: if it is not a number, die, otherwise set $agedays to $opt_d (the value of -d).

      Normally, I would run agecmd.pl -d90

      By switching the two, you have made the d option the help option, and made the -h option the number of days. By not specifying -d, $agedays is then set to 60 by default, giving you no problems.

      By taking things out, and changing the code, it's hard to know what happened. Does you have problems if you run the original code with no mods? Can you post your new code?

      if ($opt_h){ 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; } }

      -OzzyOsbourne

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://103161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-10-02 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (7 votes). Check out past polls.

    Notices?