Ruby Net :: FTP, extracting a file name from ftp.list ()

I use the following code to try to get all ftp files using Ruby.

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

The problem is that ftp.list returns a whole line of information, not just the file name, for example.

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

How to extract filen from this line?

Many thanks

+7
source share
3 answers

You can use a public nlst 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 
+16
source

If you want to handle the output of ftp.list, you can find net-ftp-list .

0
source

However, list seems useful because you can pass in an appropriate template that is not displayed, which supports nlst . I just made a quick and dirty hack to do list work:

 ftp.list("*.zip") do |zipfile| zipfile = zipfile.split(/\s+/).last # ... do something with the file end 
-2
source

All Articles