I am downloading say 100 files from ucsc website and storing it into dest folder. download.file function create a file in destination folder even if the file is not present which is something I dont want. So I wrote if condition to remove the file if the download function has non zero value. Now it exits when there is an error or file not present. How can I use "try" and "if" condition together so that the program does not exit on error and delete the created file in destination folder. for (i in 1: 100) { fileUrl = ucscfilenames[i] if (download.file(fileUrl, destFile, 'wget' , quiet = TRUE) != 0) { file.remove(destFile) } } thanks -- ------------- Mary Kindall Yorktown Heights, NY USA [[alternative HTML version deleted]]
On Nov 8, 2011, at 6:32 PM, Mary Kindall wrote:> I am downloading say 100 files from ucsc website and storing it into > dest > folder. > download.file function create a file in destination folder even if > the file > is not present which is something I dont want. > So I wrote if condition to remove the file if the download function > has non > zero value. > > Now it exits when there is an error or file not present. How can I use > "try" and "if" condition together so that the program does not exit on > error and delete the created file in destination folder. > > for (i in 1: 100) > { > fileUrl = ucscfilenames[i] > if (download.file(fileUrl, destFile, 'wget' , quiet = TRUE) != 0) > { > file.remove(destFile) > } > } >I do not see where destFile gets its values. I don't think you can just give it a path. And why would you want to remove a file that was never successfully created, anyway? Wouldn't it be something like (pseudo-) : <define a vector of destination file names> if( try(download.file(fileUrl, destFile[i], 'wget' , quiet = TRUE) ) ) { <write to error log> } else { <write to success log> } -- David Winsemius, MD West Hartford, CT
See downloadFile() of the R.utils package (on CRAN); it allows you to download to a file to a given path (without have to "replicate" the filename), and it also have an option (the default) to drop zero-size files that are (sometimes) created when download.file() tries to download a non-existing file. Moreover, the method downloads files in binary mode by default (download in text mode is a common cause for "corrupt" files). See help("downloadFile", package="R.utils") for more bells and whistles. /Henrik On Tue, Nov 8, 2011 at 3:32 PM, Mary Kindall <mary.kindall at gmail.com> wrote:> I am downloading say 100 files from ucsc website and storing it into dest > folder. > download.file function create a file in destination folder even if the file > is not present which is something I dont want. > So I wrote if condition to remove the file if the download function has non > zero value. > > Now it exits when there is an error or file not present. How can I use > "try" and "if" condition together so that the program does not exit on > error and delete the created file in destination folder. > > for (i in 1: 100) > { > fileUrl = ucscfilenames[i] > if (download.file(fileUrl, destFile, 'wget' , quiet = TRUE) != 0) > ? ?{ > ? ?file.remove(destFile) > ? ?} > } > > > > thanks > > -- > ------------- > Mary Kindall > Yorktown Heights, NY > USA > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >