[R-sig-Geo] getting subfolders in a directory on FTP (MODIS)

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Jul 14 16:53:18 CEST 2009


On Tue, Jul 14, 2009 at 2:12 PM, Tomislav
Hengl<hengl at spatial-analyst.net> wrote:

> To tell you honestly, I could not figure out how to fetch names of sub-folders from an ftp (maybe it
> is simply not possible?),

 Apparently the FTP spec doesn't specify how to return a list of
directories, which, I read, "is a source of headaches to FTP client
developers".

 If you do:

 resp = getURL("ftp://n4ftl01u.ecs.nasa.gov/SAN/MOST/MOD10CM.005/")
 items = strsplit(resp,"\n")[[1]]

you get the folders (and files) but the folder names are in the form
of a unix directory listing. So you then want the last word of any
lines that start with 'd'. Lets get the lines first:

 folderLines = items[substr(items,1,1)=='d']

that gets you all the lines. There's probably a neater way of getting
the last word than this:

 lastBit <- function(x){x[length(x)]}

 dirs = unlist(lapply(strsplit(folderLines," "),lastBit))

Note this only works on the FTP server specified, other FTP servers
are at liberty to send back any dir format they want (I think I was
surprised the first time I connected to a DOS-based FTP server and saw
a DOS DIR listing!).

Barry



More information about the R-sig-Geo mailing list