CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Ftp directory listing not working with paths containing spaces: Edit Log



turnerr
Novice

Jan 1, 2014, 7:27 AM


Views: 9432
Ftp directory listing not working with paths containing spaces

I have a script that syncs up directory structures using ftp but am having a problem with it when the path names contain spaces. I have tried everything I can think of and still cannot get it to work. Any help would be greatly appreciated.

Here's a code snippet:

Code
my $ftp = new Net::FTP ($opt_s, 
Debug => $opt_d,
Passive => $opt_P,
);

die "Failed to connect to server '$opt_s': $!\n" unless $ftp;
die "Failed to login as $opt_u\n" unless $ftp->login($opt_u, $opt_p);
die "Cannot change directory to $opt_r\n" unless $ftp->cwd($opt_r);
warn "Failed to set binary mode\n" unless $ftp->binary();

print "connected\n" if $opt_v;

sub scan_ftp
{
my $ftp = shift;
my $path = shift;
my $rrem = shift;

print "path $path\n"; if ($path =~ / /)
{
$path =~ s/ /\\ /g;
}

print "path2 $path\n";

my $t = $path;
$t =~ s/ /\\ /g;


my $rdir = length($path) ? $ftp->dir($path) : $ftp->dir();

return unless $rdir and @$rdir;
print "after return\n";

for my $f (@$rdir)
{
print "1 $f\n";
next if $f =~ m/^d.+\s\.\.?$/;

my $n = (split(/\s+/, $f, 9))[8]; next unless defined $n;


my $name = '';
$name = $path . '/' if $path;
$name .= $n;


if ($opt_i and $name =~ m/$opt_i/)
{
print "ftp: IGNORING $name\n" if $opt_d;
next;
}

next if exists $rrem->{$name};

my $mdtm = ($ftp->mdtm($name) || 0) + $opt_o;
my $size = $ftp->size($name) || 0;
my $type = substr($f, 0, 1);
$type =~ s/-/f/;

warn "ftp: adding $name ($mdtm, $size, $type)\n" if $opt_d;

$rrem->{$name} =
{
mdtm => $mdtm,
size => $size,
type => $type,
};

scan_ftp($ftp, "$name", $rrem) if $type eq 'd';
}
}



Here's some debugging output:

1 drwxr----- 2 oracle oinstall 512 Aug 26 14:48 New folder
Net::FTP=GLOB(0x31364)>>> MDTM cart/New folder^M
Net::FTP=GLOB(0x31364)<<< 550 cart/New folder: not a plain file.
Net::FTP=GLOB(0x31364)>>> SIZE cart/New folder^M
Net::FTP=GLOB(0x31364)<<< 550 cart/New folder: not a plain file.
ftp: adding cart/New folder (0, 0, d)
path cart/New folder
path2 cart/New\\ folder
Net::FTP=GLOB(0x31364)>>> PORT 192,168,7,233,176,34^M
Net::FTP=GLOB(0x31364)<<< 200 PORT command successful.
Net::FTP=GLOB(0x31364)>>> LIST cart/New\\ folder^M
Net::FTP=GLOB(0x31364)<<< 150 Opening BINARY mode data connection for /bin/ls.
Net::FTP=GLOB(0x31364)<<< 226 Transfer complete.
1 -rwxr----- 1 oracle oinstall 3129 Dec 3 2012 ShoppingCart.class
Net::FTP=GLOB(0x31364)>>> MDTM cart/ShoppingCart.class^M
Net::FTP=GLOB(0x31364)<<< 213 20121203182147
Net::FTP=GLOB(0x31364)>>> SIZE cart/ShoppingCart.class^M
Net::FTP=GLOB(0x31364)<<< 213 3129
ftp: adding cart/ShoppingCart.class (1354558907, 3129, f)
1 -rwxr----- 1 oracle oinstall 3393 Jun 18 2013 ShoppingCartAccount.class
Net::FTP=GLOB(0x31364)>>> MDTM cart/ShoppingCartAccount.class^M


This is what the directory structure in question looks like:

/usr2/prod/app/oracle/Oraas/j2ee/act_webdev/applications/act_webdev/act_webdev/WEB-INF/classes/act/payment/cart
.:
total 38
drwxr----- 2 oracle oinstall 512 Aug 26 14:48 New folder
-rwxr----- 1 oracle oinstall 3393 Jun 18 2013 ShoppingCartAccount.class
-rwxr----- 1 oracle oinstall 8491 Jun 18 2013 ShoppingCartAccountInformation.class
-rwxr----- 1 oracle oinstall 3129 Dec 3 2012 ShoppingCart.class
-rw-r----- 1 oracle oinstall 112 Jan 1 09:12 tst

./New folder:
total 28
-rwxr----- 1 oracle oinstall 3645 Dec 3 2012 ShoppingCartAccount.class
-rwxr----- 1 oracle oinstall 9596 Mar 5 2013 ShoppingCartAccountInformation.class


Thanks,
Rob


(This post was edited by FishMonger on Jan 1, 2014, 7:48 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Jan 1, 2014, 7:48 AM: Added code tags


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives