Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check the existence of a downloaded file

Tags:

r

I've created a R markdown file that starts by loading a file from the web. I found the cache=TRUE to be a little flaky so I want to put an if condition in to check for the downloaded file before downloading it.

Current Code - Always downloads file

fileURL <- "https://dl.dropbox.com/u/7710864/courseraPublic/samsungData.rda"
setInternet2(TRUE)
download.file(fileURL ,destfile="./data/samsungData.rda",method="auto")
load("./data/samsungData.rda")

Desired Code - only upload if if not already downloaded

 destfile="./data/samsungData.rda"    
 fileURL <-
 "https://dl.dropbox.com/u/7710864/courseraPublic/samsungData.rda"   
 if (destFile doesNotExist) {
    setInternet2(TRUE)
    download.file(fileURL ,destfile,method="auto") }
    load("./data/samsungData.rda")
 }
 load(destfile)

What syntax will give me the condition "destFile doesNotExist"

like image 847
user1605665 Avatar asked Feb 15 '13 23:02

user1605665


People also ask

How do I validate a downloaded file?

Verify in the Internet The service is easy to use. Just drag & drop the respective file to the drop zone in the webpage or open the file dialog. Then choose SHA256 or MD5 as checksum type, insert the checksum from the file you got from download webpage. Finally click on [Compare] to start the verification.

How do I find the origin of a downloaded file?

Right-click on the questionable file and select "Get Info" (you can also click once on the file and press command + I). Once you are in the Get Info window, click the "More Info" disclosure triangle. Look in the "Where from" field to identify your file's origin.

Where can I find active Downloads?

You can find your downloads on your Android device in your My Files app (called File Manager on some phones), which you can find in the device's App Drawer. Unlike iPhone, app downloads are not stored on the home screen of your Android device, and can be found with an upward swipe on the home screen.


3 Answers

You can use tryCatch

  if(!file.exists(destfile)){
    res <- tryCatch(download.file(fileURL,
                              destfile="./data/samsungData.rda",
                              method="auto"),
                error=function(e) 1)
    if(dat!=1) load("./data/samsungData.rda") 
}
like image 115
agstudy Avatar answered Oct 03 '22 07:10

agstudy


As per the answer given by @agstudy

 destfile="./data/samsungData.rda" 
 fileURL <-
 "https://dl.dropbox.com/u/7710864/courseraPublic/samsungData.rda"   
 if (!file.exists(destfile)) {
    setInternet2(TRUE)
    download.file(fileURL ,destfile,method="auto") }
    load("./data/samsungData.rda")
 }
 load(destfile)
like image 22
user1605665 Avatar answered Oct 03 '22 07:10

user1605665


An easy way to check the existence of a file in your working directory is: which(list.files() == "nameoffile.csv")

This doesn't exactly answer his question but I thought this might be helpful to someone who simply wants to check if a particular file is there in their directory.

like image 11
Nutan Avatar answered Oct 03 '22 06:10

Nutan