Partha sarathi
Partha sarathi

Reputation: 131

Delete empty folders inside working directory

I have number of empty folders inside a working directory. Want to remove those empty folders in R. Can somebody help me out on this.

Upvotes: 2

Views: 1126

Answers (3)

T. Austring
T. Austring

Reputation: 11

For anyone still struggling (like I was) with deleting folders in a synced SharePoint directory, add force=TRUE.

unlink("dir", recursive = TRUE, force = TRUE)

Upvotes: 1

dario
dario

Reputation: 6483

We can delete folders using:

unlink("path_to_non_empty_dir", recursive=TRUE)

Edit: removed part about non-empty folders

From ?unlink:

If recursive = FALSE directories are not deleted, not even empty ones.

Upvotes: -1

Stéphane Laurent
Stéphane Laurent

Reputation: 84709

Assuming the current directory is the one in which you want to delete the empty folders, you can do:

folders <- list.dirs(recursive = FALSE)
for(folder in folders){
  if(length(dir(folder)) == 0){
    unlink(folder, recursive = TRUE)
  }
}

Upvotes: 2

Related Questions