develooper Front page | perl.beginners | Postings from May 2007

Re: creation of directory on daily basis and store the files in it

From:
Alma
Date:
May 26, 2007 03:29
Subject:
Re: creation of directory on daily basis and store the files in it
Message ID:
1180151277.869957.98250@r19g2000prf.googlegroups.com
On May 25, 1:30 pm, "eish...@googlemail.com" <eish...@googlemail.com>
wrote:
> On May 23, 9:59 am, Alma <almatir...@gmail.com> wrote:
>
>
>
> > On May 23, 7:28 am, "eish...@googlemail.com" <eish...@googlemail.com>
> > wrote:
>
> > > On May 23, 8:14 am, "eish...@googlemail.com" <eish...@googlemail.com>
> > > wrote:
>
> > > > On May 23, 7:47 am, "eish...@googlemail.com" <eish...@googlemail.com>
> > > > wrote:
>
> > > > > On May 23, 5:13 am, Alma <almatir...@gmail.com> wrote:
>
> > > > > > Hi All,
>
> > > > > >  I have tocreatea script that creates adirectoryon day basis &
> > > > > > need to store the files uploaded on that particular day to that
> > > > > >directory.
>
> > > > > > Any help regarding it will be appreciated .
>
> > > > > > alma
>
> > > > > #!/usr/bin/perl -w
>
> > > > > use strict;
> > > > > use File::Find;
> > > > > use File::Copy;
> > > > > use Time::Local;
>
> > > > > my($day, $mon, $yr) = (localtime)[3, 4, 5];
> > > > > my $dir = $day . "_" . ($mon + 1) . "_" . ($yr + 1900); # check epoch
> > > > > if you are going to use date as dir name (UNIX $yr + 30 i think)
>
> > > > > mkdir "$dir";
> > > > > print "directorycreated: $dir\n";
>
> > > > > my $start = timelocal(0, 0, 0, $day, $mon, $yr);        # midnight today
> > > > > my $stop = $start + (24 * 60 * 60 ) - 1;
>
> > > > > sub find_uploads_today{
> > > > >         my $start = shift;
> > > > >         my $stop = shift;
> > > > >         my @files;
>
> > > > >         return (
> > > > >                 sub{
> > > > >                         my $timestamp = (stat $_)[9];
> > > > >                         push @files, $_ if  $timestamp > $start && $timestamp < $stop && -
> > > > > f;
> > > > >                 },
> > > > >                 sub{
> > > > >                         return @files;
> > > > >                 }
> > > > >         );
>
> > > > > }
>
> > > > > my($gather, $yield)  = find_uploads_today($start, $stop);
> > > > > find($gather, ".");
> > > > > my @files = $yield->(); # contains only files that are uploaded today
>
> > > > > foreach (@files)
> > > > > {
> > > > >         if ($_ ne $0){ # ensuring that if script in same dir as uploads it is
> > > > > not moved to day folder
> > > > >                 move("$_", "$dir/$_") or die "move failed: $!"; # 2nd param is
> > > > > system specific - change if using Windows
> > > > >         print "moving $_\n";
> > > > >         }
>
> > > > > }
>
> > > > you can cut these lines and place them in the sub in place of $start =
> > > > shift and $stop = shift:
> > > > my $start = timelocal(0, 0, 0, $day, $mon, $yr);        # midnight
> > > > today
> > > > my $stop = $start + (24 * 60 * 60 ) - 1;
>
> > > you could also replace the foreach loop at the bottom with:
>
> > > grep {
> > >         if ($_ ne $0){
> > >                 move("$_", "$dir/$_") or die "$!";
> > >                 print "moving $_\n";
> > >         }
>
> > > } @files;
>
> > Thanks a lot.
>
> > Can i keep the perl scrip to create a directory in cron.daily. so that
> > it gets created & while storing the files in the date folder. it
> > checks for the today's date & store it .
>
> Do you want all the uploaded files copied to the same main directory
> (moved_uploads) but separated by date?
> +some_folder
> +moved_uploads
>  |-24_May_2007
>  |-25_May_2007

yes a sort of i wanted tot keep the main folder as backup.
for ex. /home/project/backup/$_today's_dir(Which should be date) &
dump all the files uploaded today . for tommorow another folder should
get created in the location
/home/project/backup/$tommorow_dir ..





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About