How to Check the Existence of a Downloaded File

How do I check the existence of a downloaded file

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")
}

Check If File Exists Before downloading the file

I detected some strange behavior with exists time ago and changed it to isFile:

File file = new File(path);
if (file.isFile()) {
Toast.makeText(getContext(), path + "/n exists", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Downloading", Toast.LENGTH_SHORT).show();
// ...
}

I think the mobile, somehow, created a directory every time new File() was executed.
Check this.

How to check If file exists in the url before use download.file in R

You can use the HEAD request. In R it's available in package httr. The return codes can be found on the Wikipedia. This SO post may be useful.

A very simple function could be

urlFileExist <- function(url){
HTTP_STATUS_OK <- 200
hd <- httr::HEAD(url)
status <- hd$all_headers[[1]]$status
list(exists = status == HTTP_STATUS_OK, status = status)
}

lapply(x, urlFileExist)
#[[1]]
#[[1]]$exists
#[1] TRUE
#
#[[1]]$status
#[1] 200
#
#
#[[2]]
#[[2]]$exists
#[1] TRUE
#
#[[2]]$status
#[1] 200

Download and check if file exists

What you can do is every time before you download file clean a folder(Delete all files) and then after hitting url check if atleast one file is present if file is present you can check file name contains '.exe'

     // clean folder by deleting all files from download location folder
driver.get(url);

time.sleep(5)
if os.path.isfile('CHECK URL PATH FILENAME'): // get list of files present in folder ane verify if file name ends with .exe
print("File download is completed") // Write code to delete all files after this line.
else:
print("File download is not

R:Check existence of today's file and if it doesn't exist, download it

I think this an issue with encoding the result of download.file, one way could be to use fread to get the data then write it with fwrite:

#Look for COVID file starting with "data_"
destfile <- list.files(pattern = "data_")
#Find file date
fileDate <- file.info(destfile)$ctime %>% as.Date()
#>[1] "2020-11-06" "2020-11-06" "2020-11-06"

if(!length(destfile) | max(fileDate) != today()){
COVIDdata <- fread(fileURL)
fwrite(COVIDdata, file = paste0("data_",today(),".csv"))
}


Related Topics



Leave a reply



Submit