[R] remove a "corrupted file" after using download.file() with R on Windows 7

Robert Baer rbaer at atsu.edu
Thu Sep 29 21:35:17 CEST 2016


On 9/28/2016 11:32 PM, Fabien Tarrade wrote:
> Hi there,
>
> Sometime download.file() failed to download the file and I would like 
> to remove the correspond file.
No answers, but a couple of additional questions:
1)  Does the issue persist if you close R or does the file remain locked 
against deletion?
2) If so, is there a related process in the task list if you use 
CTRL-ALT-DEL?
3) Does       print(e$message) yield any useful information when it hangs?

Would debugging in R Studio shed additional light?

> The issue is that I am not able to do it and Windows complain that the 
> file is use by another application.
> I try to closeAllConnections(), or unlink() before removing the file 
> but without sucess.
>
> Any idea how I should proceed &
>
> Please find the code below
>
>  # consider warning as an error
>   options(warn=2)
>
>   # try to download the file
>   tryCatch({
>     download.file(url,path_file,mode="wb",quiet=quiet)
>     return(0)
>   },error = function(e){
>     if(verbose){
>       print(e)
>       print(e$message)
>     }
>     # close file when it failed
>     if (file.exists(path_file)){
>       closeAllConnections()
>       #unlink(path_file, recursive=TRUE)
>       #file.create(path_file,overwrite=TRUE,showWarning=TRUE)
>       #system(paste0('open "', path_file, '"'))
>       file.remove(path_file,overwrite=TRUE,showWarning=TRUE)
>     }
>     return(1)
>     }
> )
>
> Thanks a lot
> Cheers
> Fabien
>

-- 


--
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A T Still University of Health Sciences
800 W. Jefferson St
Kirksville, MO 63501
660-626-2321 Department
660-626-2965 FAX



More information about the R-help mailing list