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.
Henrique Dallazuanna
2010-Nov-30 16:12 UTC
[R] how to know if a file exists on a remote server?
Take a look on ?file_test On Tue, Nov 30, 2010 at 2:10 PM, Baoqiang Cao <bqcaomail@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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
?file.exists On Tue, Nov 30, 2010 at 11: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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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@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@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. >[[alternative HTML version deleted]]
using RCurl getFtpList <- function(ftp){ # the structure returned is dependent on the ftp site as there are # various formats for directory listings dependent upon the server # and the OS. you will need to play with this. # have a look at the ftp with your browser first and adjust accordingly. # some formats only return 4 columns. # column 1= literal string first position mean file # column 2= number 1 # column 3 =owner # column 4 = group # column 5 =file size # colmn 6 =Month # column 7 =Day # column 8 =Time (year) # column 9 =FileName # txt <- getURL(ftp) dir <- read.table( textConnection(txt),as.is=TRUE) out <- data.frame(Dir=ftp,Filename=dir[, ncol(dir)],Size=dir[ ,5], Month=dir[ ,6],Day=dir[ ,7],Time=dir[ ,8],stringsAsFactors=FALSE) closeAllConnections() return(out) } On Tue, Nov 30, 2010 at 8:10 AM, Baoqiang Cao <bqcaomail@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@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. >[[alternative HTML version deleted]]
On 30/11/10 10:10:07, Baoqiang Cao wrote:> 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?Hi Baoqiang, try downloading the file with R's download.file() function. Then you should examine the returned value. Citing a part of ?download.file below:>> Value: >> An (invisible) integer code, ?0? for success and non-zero for >> failure. For the ?"wget"? and ?"lynx"? methods this is the status >> code returned by the external program. The ?"internal"? method can >> return ?1?, but will in most cases throw an error.So if you call your download via v <- download.file(url, destfile, method="wget") and v is not equal to zero, then the file is likely to be non-existent (at least the download failed). Note: the method "internal" doesn't really change the value of v, I just tried that. With "wget" it returns "0" for success and "2048" (or some other value) for non-success. Regards, Georg. -- Research Assistant Otto-von-Guericke-Universit?t Magdeburg research at georgruss.de http://research.georgruss.de
Reasonably Related Threads
- "digits" doesn't work in format function
- lm on matrix data
- in par(mfrow=c(1, 2)), how to keep one half plot static and the other half changing
- about spearman and kendal correlation coefficient calculation in "cor"
- use pcls to solve least square fitting with constraints