Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Unable to zip the folders which are created on yesterday and the previous month folder for the same yesterdays date.

by perladdict (Chaplain)
on May 24, 2013 at 15:50 UTC ( #1035169=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

In order to take a daily backup from primary drives to the secondry drives,i am trying to impliment the script which needs to zip yestrdays date folder and previous month folder which has the same date as of yestrdays date.This i need to run daily at morning so that it shouyld zip only yestreday date folder and previous months folder which has same date as of yesterdays folder.Right now for testing i am creating the zip in the same primary drive, but once if i succeed in zipping specific folder, i can change the zip path in system sommand.

For testing perpose i have kept few folder like 20130523,20130519,20130510,20130423,20130402,20130524,20130424,etc in E drive.Among them i just wanted to zip only 20130523 and 20130423 folders.
Below is the code which i am trying to achive this
#!/usr/bin/perl use POSIX; use Utils; use ReadConfigFile; use FileHandle; $ConfigFile="E:\\rajtest\\xxx\\yyy\\data\\lib\\test.ini"; print "$ConfigFile\n"; $oConfigFile=ReadConfigFile::new($ConfigFile); if (not defined $oConfigFile){ die ("ConfigFile $ConfigFile cannot be accessed"); } $DirCoolOut=$oConfigFile->{DirOut}; print "$DirOut\n"; $AbsWinZip=$oConfigFile->{AbsWinZip}; print "$AbsWinZip\n"; $DirBakCoolOutZip=$oConfigFile->{DirBakCoolOutZip}; print "$DirOutZip\n"; $DirBakZipLog=$oConfigFile->{DirBakZipLog}; print "$DirBakZipLog\n"; CheckDir($DirBakCoolOutZip,"CREATE"); CheckDir($DirBakZipLog,"CREATE"); die ("Invalid winzip application") if not defined $Abs +WinZip; die ("Backup location $DirBakCoolOutZip") if not -e $DirBakCo +olOutZip; die ("Invalid Backup log location $DirBakZipLog") if not -e $DirBakZi +pLog; #open(FH,$DirCoolOut) or die "can't find the directory $!\n"; #while($line = <FH>) { #chomp $line; $path = $DirOut; opendir(D,"$DirCoolOut") or die "can't open the directory $!\n"; my @folders = grep {(!/^\./) &amp;&amp; (!/^\.\./) } readdir(D); $winzip = "$AbsWinZip"; $now = localtime(); #print "$now\n"; %months = ("jan" => 1,"Feb" => 2,"Mar" => 3,"Apr" => 4,"May" =>5,"Jun" + =>6,"Jul"=>7, "Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec" =>12); @now = split(" ",$now); $month = $months{$now[1]}; $year = $now[-1]; $date = $now[2]; $yesterdaydate = $date - 1; $yesterdaydate = sprintf("%.2d",$yesterdaydate); $month = sprintf("%.2d",$month); $date = sprintf("%.2d",$date); $dateformat = "$year$month$yesterdaydate"; $Yesterdayzip = $dateformat; print "$Yesterdayzip\n"; #print "$dateformat\n"; $prevmonth = $month-1; $prevmonth = sprintf("%.2d",$prevmonth); $premonthdate = "$year$prevmonth$yesterdaydate\n"; $previousmonthzip = $premonthdate; print "$previousmonthzip\n"; foreach $file (@folders) { #print "$file\n"; $abspath = "$path$file"; $abspath1 = "$path$Yesterdayzip"; if($abspath =~ /$path$Yesterdayzip/) { system("\"$winzip\" -P $abspath.zip $abspath"); } if($apspath =~ /$previousmonthzip/) { system("\"$winzip\" -P $abspath.zip $abspath"); } #$abspath2 = "$path$$previousmonthzip"; #print "$abspath\n"; #system("\"$winzip\" -P $abspath.zip $abspath"); #system("\"$winzip\" -P $abspath.zip $abspath"); #system("\"$winzip\" -P $abspath1.zip $abspath"); #system("\"$winzip\" -P $abspath2.zip $abspath"); }

From the above code if i commentout the pattern matching lines and i am able to zip all the folders in the E:drive, but when i do pattern match the folders with the yesterdays date and previous months yesterdays date, i unable to zip the specific dated files
Monks,Appriciates for your attention for this post and kindly guide me for achiving this task.Thanks in Advance

Comment on Unable to zip the folders which are created on yesterday and the previous month folder for the same yesterdays date.
Download Code
Re: Unable to zip the folders which are created on yesterday and the previous month folder for the same yesterdays date.
by poj (Curate) on May 24, 2013 at 17:40 UTC
    You could filter directly with the grep statement something like this
    #!perl use strict; use Date::Calc qw(Today Add_Delta_YMD); # calc dates my $yesterday = sprintf "%4d%02d%02d",Add_Delta_YMD(Today(),0, 0,-1); my $lastmonth = sprintf "%4d%02d%02d",Add_Delta_YMD(Today(),0,-1,-1); print "Yesterday : $yesterday\n"; print "Last Month : $lastmonth\n"; # path to folders my $path = 'c:/temp';# or $DirCoolOut opendir(D,$path) or die "Can't open the directory $path : $!\n"; my @folders = grep {/$yesterday|$lastmonth/} readdir(D); for my $folder (@folders){ print "Zipping folder $folder\n"; # zip folders }
    poj
Re: Unable to zip the folders which are created on yesterday and the previous month folder for the same yesterdays date.
by Anonymous Monk on May 24, 2013 at 19:19 UTC

    Why not make more subroutines? Like I showed few days ago?

    That way you can separately test/debug the folder listing, from folder choosing, from folder zipping ?

Re: Unable to zip the folders which are created on yesterday and the previous month folder for the same yesterdays date.
by FloydATC (Hermit) on May 25, 2013 at 21:14 UTC
    The code could use some cleaning up to become more readable but if you add
    use strict; use warnings;
    to help you spot and fix typos like

        if($apspath =~ /$previousmonthzip/) {

    then perhaps the program would do what you expect?

    Update: Well, most of the time, anyway. The method you're using to calculate yesterday's date and last month's date will work properly about 90% of the time and then fail on every 1st day of each month, every last day of half the months, and always in january. And perhaps on the last few days in March. If that's going to be a problem, you might want to look for a suitable Date module :-)

    -- Time flies when you don't know what you're doing

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1035169]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2014-07-24 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (160 votes), past polls