Re: Help.. perl script to look for and then copy over any new files in a directory!



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

.



Relevant Pages

  • Re: Translate VBScript Which gets Computer Names to Perl
    ... If the script works in VBScript, why are you trying to translate it? ... Perl, I would use VBscript. ... perldoc -f push ...
    (microsoft.public.scripting.vbscript)
  • Re: How can I execute linux commands on terminal and catch the results?
    ... I'm learning perl and I need some help. ... I want to write a script that execute commands into a linux terminal ... perldoc -f system ...
    (perl.beginners)
  • Re: Perl equivalent to unix script
    ... handy with unix and unix scripting, however, I'm terrible at perl. ... What I'm looking to do with a perl script is the equivalent of the ... perldoc -f open ...
    (comp.lang.perl.misc)
  • Re: Noob wants Q&D pointer, regexp replacement.
    ... I do perl once every 5 years. ... perldoc -f readline ... This script takes at least two file names on the command line. ... first file is read and its contents stored in $contents. ...
    (comp.lang.perl.misc)
  • Re: Net::DNS
    ... function because you still use $rr within the foreach, ... > So I can find the all these object methods in the perl docs listed below? ... > the return value of 'grep'. ... > perldoc perlboot ...
    (perl.beginners)