Jay Savage                                                    
             <[EMAIL PROTECTED]                                             
             l.com>                                                     To 
                                       "[EMAIL PROTECTED]"            
             01/24/2006 04:24          <[EMAIL PROTECTED]>            
             PM                                                         cc 
                                                                           
                                                                   Subject 
                                       Re: rename file on ftp server       
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           







On 1/24/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>              "Ryan Frantz"
>              <[EMAIL PROTECTED]
>              med-llc.com>
To
>                                        <[EMAIL PROTECTED]>,
>              01/24/2006 03:17          <beginners@perl.org>
>              PM
cc
>
>
Subject
>                                        RE: rename file on ftp server

> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, January 24, 2006 3:05 PM
> > To: beginners@perl.org
> > Subject: rename file on ftp server
> >
> > All,
> >
> > I have a task to complete for a user.  He wants me to rename a file on
> a
> > remote server so that he can pick it up.
> > I am trying to use a reg exp or a blog but this is not working.  Any
> > tricks
> > of the perl trade will be mucho appreciated!
> > The file format is OhioHealthyyymmdd[0-9].xml
> > The [0-9] is a 6 digit PID I assume but changes every time the file is
> > created.
> > So the filename for example looks like OhioHealth20040124989893.xml
> >
> > thank you!
> >
> > #!/usr/bin/perl
> > use strict;
> > use warnings;
> > use Net::FTP;
> > use MIME::Lite;
> >
> > my ($monster_date,$fref) = 0;
> >
> >         sub new_date {
> >
> >             my ($year,$month,$day) = (localtime)[5,4,3];
> >             sprintf ("%04d%02d%02d", ($year += 1900), $month+1,$day);
> >         }
> >         my $passed_new_date = new_date();
> >
> >         sub new_date_append {
> >
> >             return $monster_date = "OhioHealth$passed_new_date";
> >         }
> >        $fref = \&new_date_append();
> >
> >
> >         sub ftpme {
> >
> >                 my $remotehost="ftp.monster.com";
> >                 my $remotedir="outbound";
> >                 my $user="xxxxxxxx";
> >                 my $pass="xxxxxxxx";
> >                 my $data=$$fref;
> >                 my
> > $ftplog="/psofthr/hr88prd/intf/monster/log/interface_monster.log";
> >
> >                 open (FTPLOG, ">>$ftplog") || die "could not open
> file:
> > $ftplog $!";
> >                 my $ftp = Net::FTP->new($remotehost, Debug => 10)
> >                 || do {print FTPLOG "\tCannot connect to $remotehost:
> > monster.com: $!"; };
> >                 $ftp->login($user, $pass) || do {print FTPLOG "\tLogin
> > failed!: $!"; };
> >                 $ftp->binary();
> >                 $ftp->cwd($remotedir);
> >                 $ftp->rename("$$fref\[0-9].xml", "OhioHealth.xml") ||
> do
>
> To match one or more of the previous characters, use the '+' operator:
>
> "$$fref\[0-9]+.xml"
>
> > {print FTPLOG "\tFTP get from monster.com failed!: $!"; };
> >                 $ftp->quit;
> >         }
> >
> >        if ( $passed_new_date and $$fref ) {
> >            print "YES\n";
> >            print $passed_new_date,"\n";
> >            print $$fref,"\n";
> >            ftpme();
> >        }
> > #       else {
> > #           "email warning"
> > #       }
> >
> > Derek B. Smith
> > OhioHealth IT
> > UNIX / TSM / EDM Teams

> your solution does not work.... I am not a noob so I have tried this
> already.
> thank you though!
>
> __DEBUG__
>
> FTP is returning the RNFR command OhioHealth20060124[0-9]+.xml
> 550 OhioHealth20060124[0-9]+.xml: The system cannot find the file
> specified.
>

There are actually a couple of issues here. The one that's causing the
error is interpolation. you're passing a double-quoted string to
$ftp->rename(), so perl is doing interpolation before it passes the
string off to the system. As you've probably noticed, single-quoting
won't work, either, because then the systems gets a literal, which
yeilds more-or-less the same result. There are probably ways around
this with double escapes but why bother? Thry something like this:

    my @dir = $ftp->ls();
    my @matches = grep (/$$fref[0-9].xml/, @dir);
    $ftp->rename(@matches[0], "OhioHealth.xml") || log();

The other problem with this is that I don't see anything to prevent
multiple files from matching, or any logic to choose between the
multiple matches. $$fref0.xml ... $$fref9.xml could all exist--in
fact, I assume that the 0-9 number scheme exists because they're
expected to exist--and there is no way to know which one your getting.
Neither the system wildcard/glob nor $ftp->ls() is guaranteed to
preovide the driectory listing in any particular order.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

***********************************************************************
***********************************************************************

I was able to get this working with a similiar setup as Jay provided... thx
dude! : )
I used

foreach ($ftp->ls()) {
    if (/pattern patch/) {
        $foo =$_;
        last;
    }
}
ftp rename function.

thanks again for pointing me in the right direction!



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to