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
R Shiny: Download Existing File
Ggplot2: Geom_Text() with Facet_Grid()
Plotting Envfit Vectors (Vegan Package) in Ggplot2
Plotting a Large Number of Custom Functions in Ggplot in R Using Stat_Function()
Extract Column from Data.Frame as a Vector
Building a Tiny R Package with Cuda and Rcpp
How to Name the List of the Group_Split Output in Dplyr
Find the Probability Density of a New Data Point Using "Density" Function in R
Passing String Variable Facet_Wrap() in Ggplot Using R
Using Dplyr to Conditionally Replace Values in a Column
Shiny Saving Url State Subpages and Tabs
R Scoping: Disallow Global Variables in Function
Error with Ggplot2 Mapping Variable to Y and Using Stat="Bin"
How to Set Unique Row and Column Names of a Matrix When Its Dimension Is Unknown