For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > perl net ftp on windows









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 perl net ftp on windows
DBSMITH@OhioHealth.com

2006-01-10, 4:02 am

I cannot figure out how Net::FTP set its local current directory for gets
of remote files? Does anyone know?
I want to get remotefile from remotehost using:
but I want to place this file in $localdir not in what Net::FTP claims to
be

get ( REMOTE_FILE [, LOCAL_FILE [, WHERE]] )
Get REMOTE_FILE from the server and store locally. LOCAL_FILE may be
a filename or a filehandle. If not specified, the file will be stored
in the current directory with the same leafname as the remote file.


If WHERE is given then the first WHERE bytes of the file will not be
transferred, and the remaining bytes will be appended to the local
file if it already exists.


Returns LOCAL_FILE, or the generated local file name if LOCAL_FILE is
not given. If an error was encountered undef is returned.

please help!

thx
derek

#!/usr/bin/perl


use strict;
use warnings;
use diagnostics;
use Net::FTP;
require 5.8.0;
$ENV{"PATH"} = qq(C:\\Perl\\bin:C:\\Documents and Settings\\mh-hl7:);

my $p= qq(--passphrase-fd 0);
my $de= qq(--decrypt);
my $outp= qq(--output);
my $MHfile= qq(C:\\Documents and Settings\\mh-hl7\\MHFM.txt.asc);
my $pass= qq(C:\\temp\\pass.txt);

sub ftpme {
my $remotehost = "216.54.146.8";
my $remotedir = "cloverleaf";
my $localdir = "C:\\Documents and Settings\\mh-hl7";
my $user = "...";
my $pass = "...";
my $data = "MHFM.txt.asc";
my $ftplog = "C:\\temp\\ftpxfer_to_OH.log";

open (FTPLOG, ">$ftplog") or warn "was unable to open FTPLOG $!";
my $ftp = Net::FTP->new($remotehost, Debug => 10)
or do {print FTPLOG "Cannot connect to $remotehost $!";};
$ftp->login($user,$pass) or do {print FTPLOG "Login failed $!";};
$ftp->binary();
$ftp->cwd($remotedir);
$ftp->get($data) or do {print FTPLOG "Get data failed $!";};
$ftp->quit;
}

#unlink $MHfile;

ftpme();

if ( -s $MHfile ) {
open (PASS, "+<$pass") or warn "was unable to open FH $!";
for (;<PASS>;) {
print $_;
}
}
#else {
# print "file $MHfile not present","\n";
# system ("C:\\WINDOWS\\notepad.exe C:\\temp\\pass.txt");
#}

close (PASS) or warn "was unable to close FH $!";

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams


mtbr1221@cwnet.com

2006-01-10, 4:02 am

(this list replies to the poster, not to the list unless I specify to list--I'm
growing accustomed)
(here's one for the list) (hopefully in the future I just send to list by
remembering to specify so)

On Thu, 29 Dec 2005 13:08 , DBSMITH@OhioHealth.com sent:
>I cannot figure out how Net::FTP set its local current directory for gets
>of remote files? Does anyone know?
>I want to get remotefile from remotehost using:
>but I want to place this file in $localdir not in what Net::FTP claims to


$ftp->get($_, "$localdir$_") or die "get failed ", $ftp->message;

Sorry, I don't use Win much. That worked for me on Linux.

also, instead of using LOCAL_FILE
I used chdir builtin function (2 different ways to do it). see next code
(includes those 2 different ways to do it) it downloads 3 small html files from a
CPAN mirror site. Might be easier just to chdir to the dir where you want the
file to be saved (since it saves in C-urrent W-orking D-irectory if given no
specification otherwise).

#!/usr/bin/perl -w

use strict;
use Net::FTP;
use Cwd;

my $ch_dir = '/home/al/tmp2';

print "cur_dir is:\n";
print cwd, "\n\n";

chdir( $ch_dir ) or die "Cant chdir to $ch_dir $!";
print "cur_dir is:\n";
print cwd, "\n";

my $ftp = Net::FTP->new("cpan.mirrors.tds.net", Debug => 0)
or die "Cannot connect to some.host.name: $@";

$ftp->login("anonymous",'-anonymous@')
or die "Cannot login ", $ftp->message;

$ftp->cwd("/pub/CPAN")
or die "Cannot change working directory ", $ftp->message;


my @files=$ftp->ls
or die "Cannot open the directory",$ftp->message;
# print @files, "\n\n";

print "-------------------------------------------------\n";
print "Here are \@files:\n";
foreach ( @files ) {
print $_, "\n";
}
print "-------------------------------------------------\n";

print "Here are readme\*:\n";
foreach ( @files ) {
print $_, "\n" if /readme/i;
}
print "-------------------------------------------------\n";

my $localdir = '/home/al/tmp2/';
my @got_list;
print "Here are get\/got list:\n";
foreach ( @files ) {
chomp;
if ( /(^rea|^si|^di).*\.html/i ) { # get these
push @got_list, $_;

# use either but not both of the next two lines
$ftp->get($_) or die "get failed ", $ftp->message;
# $ftp->get($_, "$localdir$_") or die "get failed ", $ftp->message;
}
}
print @got_list, "\n";
# end

--
Alan.


---- Msg sent via CWNet - http://www.cwnet.com/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com