Home > Archive > PERL Beginners > January 2006 > rename file on ftp server
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 |
rename file on ftp server
|
|
| DBSMITH@OhioHealth.com 2006-01-24, 6:56 pm |
| 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
{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
| |
| Ryan Frantz 2006-01-24, 6:56 pm |
|
> -----Original Message-----
> From: DBSMITH@OhioHealth.com [mailto:DBSMITH@OhioHealth.com]
> Sent: Tuesday, January 24, 2006 3:05 PM
> To: beginners@perl.org
> Subject: rename file on ftp server
>=20
> All,
>=20
> 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
>=20
> thank you!
>=20
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Net::FTP;
> use MIME::Lite;
>=20
> my ($monster_date,$fref) =3D 0;
>=20
> sub new_date {
>=20
> my ($year,$month,$day) =3D (localtime)[5,4,3];
> sprintf ("%04d%02d%02d", ($year +=3D 1900), =
$month+1,$day);
> }
> my $passed_new_date =3D new_date();
>=20
> sub new_date_append {
>=20
> return $monster_date =3D "OhioHealth$passed_new_date";
> }
> $fref =3D \&new_date_append();
>=20
>=20
> sub ftpme {
>=20
> my $remotehost=3D"ftp.monster.com";
> my $remotedir=3D"outbound";
> my $user=3D"xxxxxxxx";
> my $pass=3D"xxxxxxxx";
> my $data=3D$$fref;
> my
> $ftplog=3D"/psofthr/hr88prd/intf/monster/log/interface_monster.log";
>=20
> open (FTPLOG, ">>$ftplog") || die "could not open
file:
> $ftplog $!";
> my $ftp =3D Net::FTP->new($remotehost, Debug =3D> 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;
> }
>=20
> if ( $passed_new_date and $$fref ) {
> print "YES\n";
> print $passed_new_date,"\n";
> print $$fref,"\n";
> ftpme();
> }
> # else {
> # "email warning"
> # }
>=20
> Derek B. Smith
> OhioHealth IT
> UNIX / TSM / EDM Teams
>=20
>=20
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
| |
| DBSMITH@OhioHealth.com 2006-01-24, 6:56 pm |
|
"Ryan Frantz"
<ryanfrantz@infor
med-llc.com> To
<DBSMITH@OhioHealth.com>,
01/24/2006 03:17 <beginners@perl.org>
PM cc
Subject
RE: rename file on ftp server
> -----Original Message-----
> From: DBSMITH@OhioHealth.com [mailto:DBSMITH@OhioHealth.com]
> 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
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
****************************************
*************************
****************************************
*************************
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.
| |
| DBSMITH@OhioHealth.com 2006-01-24, 6:56 pm |
|
Jay Savage
<daggerquill@gmai
l.com> To
"DBSMITH@ohiohealth.com"
01/24/2006 04:24 <DBSMITH@ohiohealth.com>
PM cc
Subject
Re: rename file on ftp server
On 1/24/06, DBSMITH@ohiohealth.com <DBSMITH@ohiohealth.com> wrote:
>
> "Ryan Frantz"
> <ryanfrantz@infor
> med-llc.com>
To
> <DBSMITH@OhioHealth.com>,
> 01/24/2006 03:17 <beginners@perl.org>
> PM
cc
>
>
Subject
> RE: rename file on ftp server
> a
> file:
> do
>
> To match one or more of the previous characters, use the '+' operator:
>
> "$$fref\[0-9]+.xml"
>
[color=darkred]
> 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!
| |
| JupiterHost.Net 2006-01-25, 6:57 pm |
| *Please* remove that header and post inline properly.
[color=darkred]
Good good ;p
[color=darkred]
a) this is not a regex so [0-9] does no good unless the FTP server
understands it as a regex, in which case it will need to be a regex for
the FTP server not necessarily a Perl regex
b) Its being interpolated in the double quotes
So you're basically asking for a file that literally has a [0-9] in its
name (pronounced "bracket zero dash nine bracket" not "zero through nine")
Perhaps Net::FTP::File's directory hash will help you sort out the names
easier...
| |
| JupiterHost.Net 2006-01-25, 6:57 pm |
| > I am sorry... what do you mean "*Please* remove that header and post inline
> properly."
This is at the top of every email you send:
"JupiterHost.Net"
<mlists@jupiterho
st.net> To
beginners@perl.org
01/25/2006 09:41 cc
AM
Subject
Re: rename file on ftp server
All of that info is already in the headers if we want it and it just
looks ugly and wastes bandwidth and time to see if its relevant or not
to the issue.
> I though bottowm posts are accepted? What am I doing wrong?
Bottom posting is ok, but don't leave in the *entire* email. Only leave
what is relevant to what you are commenting on. Take this email as a for
instance :)
|
|
|
|
|