Hans-Peter
2006-Nov-28 14:21 UTC
[R] delete content of directory - unlink doesn't work as expected
Hi, I try to delete the files in a directory. While the command invisible(lapply( list.files( "DeleteThis" ), function(x) file.remove(paste("DeleteThis", x, sep="/" )) )) works, I keep thinking that there should be a more direct command. "unlink" looks like a good candidate but whith this I only arrive to either delete the whole directory or nothing happens at all. Code: file.exists( "DeleteThis" ) # TRUE: folder is here (unlink( "DeleteThis/*.*" )) # 0, content doesn't get deleted (unlink( "DeleteThis/*" )) # do. (unlink( "DeleteThis/*.*", TRUE )) # do. (unlink( "DeleteThis/*", TRUE )) # do. unlink( "DeleteThis", TRUE ) # whole folder will be deleted According to the help placeholders are allowed so I don't see where is an error. Thanks for your help! -- Regards, Hans-Peter
Gabor Grothendieck
2006-Nov-28 14:33 UTC
[R] delete content of directory - unlink doesn't work as expected
Maybe: file.remove(file.path("myDir", list.files("myDir"))) or here <- setwd("myDir") file.remove(list.files()) setwd(here) On 11/28/06, Hans-Peter <gchappi at gmail.com> wrote:> Hi, > > I try to delete the files in a directory. While the command > > invisible(lapply( list.files( "DeleteThis" ), function(x) > file.remove(paste("DeleteThis", x, sep="/" )) )) > > works, I keep thinking that there should be a more direct command. > > "unlink" looks like a good candidate but whith this I only arrive to > either delete the whole directory or nothing happens at all. Code: > > file.exists( "DeleteThis" ) # TRUE: folder is here > > (unlink( "DeleteThis/*.*" )) # 0, content doesn't get deleted > (unlink( "DeleteThis/*" )) # do. > (unlink( "DeleteThis/*.*", TRUE )) # do. > (unlink( "DeleteThis/*", TRUE )) # do. > > unlink( "DeleteThis", TRUE ) # whole folder will be deleted > > According to the help placeholders are allowed so I don't see where is an error. > > Thanks for your help! > > -- > Regards, > Hans-Peter > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Eric Thompson
2006-Nov-28 14:44 UTC
[R] delete content of directory - unlink doesn't work as expected
This has worked for me: system("rm path/*") On 11/28/06, Hans-Peter <gchappi at gmail.com> wrote:> Hi, > > I try to delete the files in a directory. While the command > > invisible(lapply( list.files( "DeleteThis" ), function(x) > file.remove(paste("DeleteThis", x, sep="/" )) )) > > works, I keep thinking that there should be a more direct command. > > "unlink" looks like a good candidate but whith this I only arrive to > either delete the whole directory or nothing happens at all. Code: > > file.exists( "DeleteThis" ) # TRUE: folder is here > > (unlink( "DeleteThis/*.*" )) # 0, content doesn't get deleted > (unlink( "DeleteThis/*" )) # do. > (unlink( "DeleteThis/*.*", TRUE )) # do. > (unlink( "DeleteThis/*", TRUE )) # do. > > unlink( "DeleteThis", TRUE ) # whole folder will be deleted > > According to the help placeholders are allowed so I don't see where is an error. > > Thanks for your help! > > -- > Regards, > Hans-Peter > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Prof Brian Ripley
2006-Nov-28 15:06 UTC
[R] delete content of directory - unlink doesn't work as expected
Similar examples to yours work for me. Please tell us your OS and version of R, and, as the footer asks PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. On Tue, 28 Nov 2006, Hans-Peter wrote:> Hi, > > I try to delete the files in a directory. While the command > > invisible(lapply( list.files( "DeleteThis" ), function(x) > file.remove(paste("DeleteThis", x, sep="/" )) ))Oh, dear! file.path() exists for a reason, and file.remove is vectorized. Try file.remove(dir("DeleteThis", full.names=TRUE))> works, I keep thinking that there should be a more direct command. > > "unlink" looks like a good candidate but whith this I only arrive to > either delete the whole directory or nothing happens at all. Code: > > file.exists( "DeleteThis" ) # TRUE: folder is here > > (unlink( "DeleteThis/*.*" )) # 0, content doesn't get deleted > (unlink( "DeleteThis/*" )) # do. > (unlink( "DeleteThis/*.*", TRUE )) # do. > (unlink( "DeleteThis/*", TRUE )) # do. > > unlink( "DeleteThis", TRUE ) # whole folder will be deleted > > According to the help placeholders are allowed so I don't see where is > an error. > > Thanks for your help! > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595