[R] how to know if a file exists on a remote server?

Baoqiang Cao bqcaomail at gmail.com
Tue Nov 30 19:09:20 CET 2010


Thanks Steven again!
I have to say that these codes are fairly sophisticated to me, but I
enjoy using already!

BC

On Tue, Nov 30, 2010 at 12:02 PM, steven mosher <moshersteven at gmail.com> wrote:
> No problem, you can also  get the directory with a curlOption of dirlistonly
>
> see the example code in the package. This will depend on the version of
> libcurl that you have.
>
> If you have an older version, my code will get you the directory.
>
> From the Rcurl examples:
>
> the files within a directory.
> url =
> 'ftp://ftp.wcc.nrcs.usda.gov/data/snow/snow_course/table/history/idaho/'
> filenames = getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE)
>
>   # Deal with newlines as \n or \r\n. (BDR)
>   # Or alternatively, instruct libcurl to change \n's to \r\n's for us with
> crlf = TRUE
>   # filenames = getURL(url, ftp.use.epsv = FALSE, ftplistonly = TRUE, crlf =
> TRUE)
> filenames = paste(url, strsplit(filenames, "\r*\n")[[1]], sep = "")
> con = getCurlHandle( ftp.use.epsv = FALSE)
> contents = sapply(filenames[1:5], getURL, curl = con)
> names(contents) = filenames[1:length(contents)]
>
>
> On Tue, Nov 30, 2010 at 9:56 AM, Baoqiang Cao <bqcaomail at gmail.com> wrote:
>>
>> Thanks Steven!
>> It is excellent code indeed!
>>
>> On Tue, Nov 30, 2010 at 11:26 AM, steven mosher <moshersteven at gmail.com>
>> wrote:
>> >  I would use RCurl.
>> >
>> >  if you have, for example, the url of an ftp site you can merely do a
>> > getURL() and the contents will be returned. That call will return data
>> > that
>> > can be coerced into a data.frame that will look like a directory
>> > structure
>> > listing the file names.
>> >
>> > If you need code just ask, but the RCurl docs are pretty good.
>> >
>> >
>> >
>> > On Tue, Nov 30, 2010 at 8:10 AM, Baoqiang Cao <bqcaomail at gmail.com>
>> > wrote:
>> >>
>> >> Hi,
>> >>
>> >> I'd like to download some data files from a remote server, the problem
>> >> here is that some of the files actually don't exist, which I don't
>> >> know before try. Just wondering if a function in R could tell me if a
>> >> file exists on a remote server? I searched this mailing list and after
>> >> read severals mails, still clueless.  Any help will be highly
>> >> appreciated.
>> >>
>> >> B.C.
>> >>
>> >> ______________________________________________
>> >> R-help at r-project.org mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>> >> PLEASE do read the posting guide
>> >> http://www.R-project.org/posting-guide.html
>> >> and provide commented, minimal, self-contained, reproducible code.
>> >
>> >
>
>



More information about the R-help mailing list