Re: Help.. perl script to look for and then copy over any new files in a directory!
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 10 Jan 2006 07:50:31 -0800
MegaC wrote:
> I am new to this group. Nice to meet everyone.
>
> I have a question.. I used to know how to do this but I forget ;(
>
> OVERVIEW
> ==========
> I am trying to write a perl script that will open a directory, and then
> copy all new files to a temp directory.
>
> ALGORITHM
> =========
> 1) Run script
> 2) WHILE script is running, REPEAT the following until <Ctrl C> is
> pressed:
while (1) {
# all your code in here. . .
}
You may also want to set up a handler for CTRL-C, something like:
$SIG{INT} = sub {
die "Received a SIGINT (CTRL-C). Terminating\n";
};
(note you have to put that *above* the while loop)
> a) Open Directory
perldoc -f opendir
my $dir = '/directory/to/open';
opendir my $dh, $dir or die "Cannot open directory $dir: $!";
> b) Look for any new files (based on time stamp or something like
> that..not sure yet..ideas are welcome :)
perldoc -f -X
perldoc -f grep
perldoc -f readdir
my @files = grep { -f "$dir/$_" and -M _ < 5 } readdr $dh;
#Change 5 as appropriate if you want files older or newer than five
days...
> c) Copy any new files to a temp directory
perldoc File::Copy
for my $file (@files){
copy "$dir/$file", '/tmp/' or die "Could not copy $file to /tmp: $!";
}
> I think I need to do a combination of opendir, readdir, closedir, but I
> am not sure how to do this. I am using Perl 5. Can anyone offer any
> advice or some code?
You may wish to familiarize yourself with some Perl basics:
http://learn.perl.org
http://perldoc.perl.org/perlintro.html
Hope this helps,
Paul Lalli
.
- Follow-Ups:
- References:
- Prev by Date: Re: ^M
- Next by Date: compiling DateTime module on SCO OSR5
- Previous by thread: Help.. perl script to look for and then copy over any new files in a directory!
- Next by thread: Re: Help.. perl script to look for and then copy over any new files in a directory!
- Index(es):
Relevant Pages
|