Reputation: 131
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
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
Reputation: 6483
We can delete folders using:
unlink("path_to_non_empty_dir", recursive=TRUE)
From ?unlink
:
If recursive = FALSE directories are not deleted, not even empty ones.
Upvotes: -1
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