3

I want to delete a file, and once that file is deleted continue with my R script.

For example:

Create a folder with a .csv in it

dir <- getwd()
dir.create(file.path(dir, "folder"), showWarnings = FALSE)
df <- matrix(1:100000,nrow=1000)
write.csv(df,file.path(dir,"folder/test.csv"))

Now if I run unlink() it will not wait until the unlink is done to execute the next line. This leaves file.still.there as TRUE

unlink(file.path(dir, "folder"),recursive = T)
file.still.there <- file.exists(file.path(dir, "folder"))

I know I can use Sys.sleep(), but is there a way to do this dynamically. Depending on the size of the folder it seems to take longer to delete.

ColinTea
  • 998
  • 1
  • 9
  • 15
  • An ugly option that I hesitate to suggest even would be a `while` loop that checks `file.exists` on the directory. But my gut tells me that that's probably a pretty terrible solution. – joran Oct 20 '16 at 21:08
  • Hmm. `file.still.there` evaluates to `FALSE` for me, on my fairly slow Windows laptop, running R-3.3.1. – Josh O'Brien Oct 20 '16 at 21:11
  • maybe try a creating a bigger .csv, I will get `FALSE` if the file is small enough – ColinTea Oct 20 '16 at 21:15
  • Well, I just made it 100 times larger and I still get `FALSE`. Do larger files really take longer to unlink? – Josh O'Brien Oct 20 '16 at 21:26

0 Answers0