l i v e c o d e

Getting a Directory Listing From an FTP Server
Mac OS 9 Mac OS X Windows Linux

>Currently I'm trying to get a listing of filenames in a directory on
>a remote server. Not having any luck and nothing in libUrl sticks
>out as being the *one* solution. Anybody have a answer for this one?
>
>Mr. Cragg....?

Good morning, Mr. Lord ... :)

For ftp urls, just use get url and provide a url that ends in "/".

put "ftp://ftp.somewhere.com/elsewhere/" into tUrl
get url tUrl
What you get with an http url will depend on the server configuration. Usually the index.html page if it exists, and possibly an html-formatted directory listing it doesn't. But not all http servers return directory listings.

Posted 8/2/2002 by Dave Cragg to the MetaCard List

Addendum 2/23/06 by Ken Ray: Don't forget that you would need to provide the user name and password for most FTP servers, so here's a generic example as a one-liner:
put url "ftp://username:password@ftp.somewhere.com/elsewhere/" into tListing
What you get back is a true directory listing, that is delimited by spaces, as in:

drwxr-x--x 6 iedev sonsothundergrp 4096 Feb 7 03:37 .
drwxr-xr-x 923 root root 24576 Feb 23 10:06 ..
-rw-r--r-- 1 iedev sonsothundergrp 24 Oct 7 19:02 .bash_logout
-rw-r--r-- 1 iedev sonsothundergrp 191 Oct 7 19:02 .bash_profile
-rw-r--r-- 1 iedev sonsothundergrp 124 Oct 7 19:02 .bashrc
drwxrwxr-x 3 iedev sonsothundergrp 4096 Feb 22 05:54 My Folder
drwxrwxr-x 5 iedev sonsothundergrp 4096 Oct 15 05:27 My Other Folder
drwxrwxrwx 3 iedev sonsothundergrp 4096 Feb 22 17:04 Her Folder
drwxrwxr-x 3 iedev sonsothundergrp 4096 Feb 8 04:55 Another Folder
lrwxrwxrwx 1 iedev sonsothundergrp 17 Oct 7 19:02 www -> /www/sonsothunder
Note also that the date column does not include the year (at least not on my Linux server). To parse this, here's some code that can help extract the data into a tab-delimited format for display in a table field and converts the date to appropriate "short date and time" format:
on mouseUp
  put url "ftp://username:password@ftp.somewhere.com/elsewhere/" into tListing
  put stsFormatFTPListing(tListing) into fld 2
end mouseUp

function stsFormatFTPListing pData
  put "" into tNewData
  repeat for each line tLine in pData
    put replaceText(word 1 to 5 of tLine," +",tab) & tab after tNewData
    put replaceText(word 6 to 8 of tLine," +"," ") into tDateTime
    put _stsFixDateTime(tDateTime) & tab after tNewData
    put word 9 to (the number of words of tLine) of tLine & cr after tNewData
  end repeat
  delete char -1 of tNewData
  return tNewData
end stsFormatFTPListing

function _stsFixDateTime pDateTime
  -- Checks the date on the server to see if it is later than the current date;
  -- if so, it subtracts a year, otherwise we leave it alone
  put word -1 of the long date into tYear
  put word 1 to 2 of pDateTime into tDate
  put word -1 of pDateTime into tTime
  
  put tDate & ", " & tYear && tTime into tTest
  convert tTest to seconds
  if the seconds < tTest then  -- "future", so subtract 1
    put tYear - 1 into tYear
  end if
  
  put tDate & ", " & tYear && tTime into tDateTime
  convert tDateTime to short date and time
  return tDateTime
end _stsFixDateTime

 Print this tip

News and Rumors Products Services Developer Resources Contact STS About STS