[R] delete content of directory - unlink doesn't work as expected

Hans-Peter gchappi at gmail.com
Tue Nov 28 15:21:27 CET 2006


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



More information about the R-help mailing list