Home > Archive > PERL Beginners > May 2007 > creation of directory on daily basis and store the files in it
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
creation of directory on daily basis and store the files in it
|
|
|
| Hi All,
I have to create a script that creates a directory on day basis &
need to store the files uploaded on that particular day to that
directory.
Any help regarding it will be appreciated .
alma
| |
| eishbut@googlemail.com 2007-05-23, 4:00 am |
| On May 23, 5:13 am, Alma <almatir...@gmail.com> wrote:
> Hi All,
>
> I have to create a script that creates a directory on 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;
my ($day, $month, $year) = (split / /, localtime)[2, 1, 4];
my $dir = "$day $month $year";
mkdir "$day-$month-$year";
| |
| eishbut@googlemail.com 2007-05-23, 4:00 am |
| On May 23, 5:13 am, Alma <almatir...@gmail.com> wrote:
> Hi All,
>
> I have to create a script that creates a directory on 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;
my ($day, $month, $year) = (split / /, localtime)[2, 1, 4];
mkdir "$day $month $year";
| |
| eishbut@googlemail.com 2007-05-23, 4:00 am |
| On May 23, 5:13 am, Alma <almatir...@gmail.com> wrote:
> Hi All,
>
> I have to create a script that creates a directory on day basis &
> need to store the files uploaded on that particular day to that
> directory.
>
> Any help regarding it will be appreciated .
>
> alma
This will get you closer to your goal.
#!/usr/bin/perl -w
use strict;
use File::Find;
use Time::Local;
my($day, $mon, $yr) = (localtime)[3, 4, 5]; # delete if you have
specific folder names for folders
my $dir = $day . "_" . ($mon + 1) . "_" . ($yr + 1900); # 1900 might
need changing - system specific (UNIX - think its $yr + 30)
mkdir "$dir";
my $start = timelocal(0, 0, 0, $day, $mon, $yr); # midnight today
my $stop = $start + (24 * 60 * 60 ) - 1;
my $uploaded_dir = "."; # dir files are initially uploaded to
my($criteria, $yield) = find_uploads_today($start, $stop);
find($criteria, $uploaded_dir);
my @files = $yield->(); # contains file that were uploaded today only
01:00 - 23:59:59
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;
}
);
}
| |
| eishbut@googlemail.com 2007-05-23, 4:00 am |
| On May 23, 5:13 am, Alma <almatir...@gmail.com> wrote:
> Hi All,
>
> I have to create a script that creates a directory on 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 "directory created: $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";
}
}
| |
| eishbut@googlemail.com 2007-05-23, 4:00 am |
| On May 23, 7:47 am, "eish...@googlemail.com" <eish...@googlemail.com>
wrote:
> On May 23, 5:13 am, Alma <almatir...@gmail.com> wrote:
>
>
>
>
>
> #!/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 "directory created: $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;
| |
| eishbut@googlemail.com 2007-05-23, 4:00 am |
| On May 23, 7:47 am, "eish...@googlemail.com" <eish...@googlemail.com>
wrote:
> On May 23, 5:13 am, Alma <almatir...@gmail.com> wrote:
>
>
>
>
>
> #!/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 "directory created: $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;
| |
| eishbut@googlemail.com 2007-05-23, 4:00 am |
| On May 23, 7:47 am, "eish...@googlemail.com" <eish...@googlemail.com>
wrote:
> On May 23, 5:13 am, Alma <almatir...@gmail.com> wrote:
>
>
>
>
>
> #!/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 "directory created: $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;
| |
| eishbut@googlemail.com 2007-05-23, 4:00 am |
| 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:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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;
| |
|
| 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:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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 .
| |
|
| 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:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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 .
| |
| eishbut@googlemail.com 2007-05-25, 9:59 pm |
| On May 23, 9:59 am, Alma <almatir...@gmail.com> wrote:
> On May 23, 7:28 am, "eish...@googlemail.com" <eish...@googlemail.com>
> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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
| |
|
| On May 25, 1:30 pm, "eish...@googlemail.com" <eish...@googlemail.com>
wrote:
> On May 23, 9:59 am, Alma <almatir...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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 ..
|
|
|
|
|