954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Store list of files from FTP to array

Hey Daniweb,

I am looking to FTP into a server, take a list of filenames, and store them into an array, so I can eventually write them into a text file.

I am currently having an issue with the storing of the ls into a local array:

use Net::FTP;
$ftp = Net::FTP->new("slawas1");
$ftp->login('username', 'password');
$ftp->cwd("/directory/path");
my @filenames=$ftp->nlst('*');
$ftp->quit;
print @filenames;


When I print it out, I get the following output:
Net::FTP::A=GLOB(0x20513318)

If you guys have any input that would be great.

Thanks!

winky
Light Poster
38 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

I should specify that the file that I ultimately want to write the list of text files to would be a local test file. I have looked through the NET::FTP library and haven't been able to find anything that might assist with this.

I basically want to mock the unix command "ls -1 blank.txt"

winky
Light Poster
38 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Hmm... I'm not familiar with the 'nlst' command. What happens if you use 'ls' instead? For me, 'ls' gives me a directory listing, neatly stored in @filenames.

Gromit
Posting Whiz in Training
212 posts since Sep 2008
Reputation Points: 47
Solved Threads: 31
 

If I remember correctly nlst returns just the filenames, while ls will return a multitude of information. For my purposes, all I need is the file name including extension.

winky
Light Poster
38 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

You might want to give 'ls' a try! In my test (using your script) 'ls' returned just filenames.

use Net::FTP;
$ftp = Net::FTP->new("mysite.com");
$ftp->login('xxxx', 'xxxx');
$ftp->cwd("/private/test");
my @filenames=$ftp->ls();
$ftp->quit;
foreach (@filenames){
  print "$_\n";
}


This results in the output:

$ perl test.pl 
test5.txt
test4.txt
test2.txt
test1.txt
test3.txt


My ftp server doesn't even recognize an 'nlst' command.
I hope this helps!
-G

Gromit
Posting Whiz in Training
212 posts since Sep 2008
Reputation Points: 47
Solved Threads: 31
 

So weird... Totally worked.. Thanks for your help. Upvotes for you!

winky
Light Poster
38 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Awesome, thanks! Glad I could be of service :)

Gromit
Posting Whiz in Training
212 posts since Sep 2008
Reputation Points: 47
Solved Threads: 31
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You