Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Net::FTP, extract filename from ftp.list()

I'm using the following code to try and get all files from ftp using Ruby.

files = ftp.list()

files.each do |file|
  ftp.gettextfile(file)
end

The problem is ftp.list returns a whole line of information, not just the filename e.g.

-rw-r--r-- 1 ftp ftp              0 May 31 11:18 brett.txt

How do I extract the filname from this string?

Many thanks

like image 601
Brettski Avatar asked May 31 '11 01:05

Brettski


1 Answers

You can use the nlst public method like this

files = ftp.nlst("*.zip")|ftp.nlst("*.txt")|ftp.nlst("*.xml")

#optionally exclude falsely matched files
exclude = /\.old|temp/ 

#exclude files with 'old' or 'temp' in the name
files = files.reject{ |e| exclude.match e } #remove files matching the exclude regex

files.each do |file|
  #do something with each file here
end
like image 65
Jamie Buchanan Avatar answered Oct 22 '22 03:10

Jamie Buchanan