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

Georg Ruß research at georgruss.de
Tue Nov 30 20:34:07 CET 2010


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



More information about the R-help mailing list