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: Advanced:
NET::FTP Dir Listing Problem

 



benchivers
Novice

Jan 4, 2003, 8:53 AM

Post #1 of 7 (1448 views)
NET::FTP Dir Listing Problem Can't Post

Hi all,

I am having a problem using the Net::Ftp.pm module. I can successfully connect to an ftp server, and retrieve the directory listing in an array, but I can not retrieve the attributes on the file e.g. file permissions, modification age into variables.

@dir = $ftp->dir();

This retrieves information on a file, and if I do:

print "@dir[0]";

it will print all the information and attributes of the file, but I would like to assign each attribute to a variable. e.g.

$chmod = @stat[0];

Is there anyway of doing this? I tried to use the split function but it would be no good as there is no key character to split it by.

Any help with this problem, would be most appreciated.

Thanks in advance,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


davorg
Thaumaturge / Moderator

Jan 4, 2003, 11:29 PM

Post #2 of 7 (1438 views)
Re: [benchivers] NET::FTP Dir Listing Problem [In reply to] Can't Post

You may be able to write a regular expression that extracts the data you want from the directory line. Or another, probably simply, solution is to use the unpack function to extract the data.

In boh cases, you should be aware that the format of the directory listing isn't standard and will change between different FTP servers. This means that the regular expression or unpack template that you use will need to change as you connect to different servers.

For example, for one of my servers I'd use something like this:

Code
my @files; 

my @cols = qw(stats links owner group size date name);
my $unpack = '10a4a5a7a15a12a*a';

foreach ($ftp->dir) {
my %rec;
@rec{@cols} = unpack $unpack, $_;
push @files, \%rec;
}

After running this code I have an array of hash references. Each of the hashes contains data about one of the files.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


benchivers
Novice

Jan 5, 2003, 11:59 PM

Post #3 of 7 (1432 views)
Re: [davorg] NET::FTP Dir Listing Problem [In reply to] Can't Post

Hi Dave,

Thanks for your reply.

The code you have provided works fine until the interpreter reaches unpack. In my server error log is says 'Invalid Argument'. I simply copied the code provided and just added it into mine. Do you know what the problem might be?

I also have no idea how the code works, the only time I have had use for 'pack' and 'unpack' was for converting char to ascii and vice-versa. Please explain the code, I understand the foreach loop, but I have no idea what 'unpack' does to the variable in the for loop.

As I couldn't get it working I finally used:

foreach ($ftp->dir()){

@statarray = split(" ", $_, 9);

}

This gave me an array of stats for the file. It also works on filenames with spaces. Is this a good way of doing it?

Thanks for the help.

Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


davorg
Thaumaturge / Moderator

Jan 6, 2003, 1:40 AM

Post #4 of 7 (1430 views)
Re: [benchivers] NET::FTP Dir Listing Problem [In reply to] Can't Post


In Reply To
The code you have provided works fine until the interpreter reaches unpack. In my server error log is says 'Invalid Argument'. I simply copied the code provided and just added it into mine. Do you know what the problem might be?


Oops yeah. I got the templates the wrong way round. Where I had

Code
my $unpack = '10a4a5a7a15a12a*a';

It should have been

Code
my $unpack = 'a10a4a5a7a15a12a*';

Sorry for the confusion. you would probably have been able to work out the problem yourself if you'd read the documentation for unpack which I directed you to.


In Reply To
I also have no idea how the code works, the only time I have had use for 'pack' and 'unpack' was for converting char to ascii and vice-versa. Please explain the code, I understand the foreach loop, but I have no idea what 'unpack' does to the variable in the for loop.


Again, reading the documentation for unpack would be a good place to find out what "unpack" does :)

Oh, and at the top of the page this site has just shown me an advert for that wonderful book Data Munging with Perl which has some great explainations on how "pack" and "unpack" work :)


In Reply To
As I couldn't get it working I finally used:

Code
foreach ($ftp->dir()){  
@statarray = split(" ", $_, 9);
}

This gave me an array of stats for the file. It also works on filenames with spaces. Is this a good way of doing it?


Yeah that seems ok. I'd be a bit more explicit about what I'm doing tho' and use

Code
split /\s+/, $_, 9;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


benchivers
Novice

Jan 11, 2003, 2:24 AM

Post #5 of 7 (1410 views)
Re: [davorg] NET::FTP Dir Listing Problem [In reply to] Can't Post

Hi Dave,

Thanks for the reply.

I am having trouble with another command I would like to send to the remote web server using net::ftp.pm, I would like to change the CHMOD settings on the file, but there is no command for it in ftp.pm.

I tried the following:

$command = "chmod 0777 foldername";

$ftp->quot($command);

but it didn't work and it didn't return an error either.

Your help is very much appreciated.

Many Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


Paul
Enthusiast

Jan 11, 2003, 3:14 AM

Post #6 of 7 (1408 views)
Re: [benchivers] NET::FTP Dir Listing Problem [In reply to] Can't Post

Read the libnet perl pod :)

http://www.perldoc.com/perl5.6.1/lib/Net/libnetFAQ.html#Using-Net--FTP

Scroll down to:

"How can I change the permissions of a file on a remote server ?"


(This post was edited by Paul on Jan 11, 2003, 3:15 AM)


benchivers
Novice

Jan 11, 2003, 8:20 AM

Post #7 of 7 (1381 views)
Re: [Paul] NET::FTP Dir Listing Problem [In reply to] Can't Post

Thanks Paul
Wheres the damn coffee? zzzZZZZZ!!!

 
 


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

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