Search code examples
rnesteddirectorycreate-directory

create nested folders in current directory in R?


I want to create nested folders, I used dir.create() to create say /test1/project/code/example/ in current directory

cidr <- getwd()
mkfldr <- "/test1/project/code/example"
dir.create(cidr,mffldr)

but it generate like

Warning message: In dir.create(file.path(cidr, mkfldr)) : cannot create dir 'C:\Users\sharmb5\Documents\R script_RR\test1\project\code/example', reason 'No such file or directory'

and there is no creation of folders. If I use showWarning = FALSE, now it not giving the above warning but still not required folders are there.

suggest any method to create all these folders all in once instead of creating one by one.

Appreciations to your answer in advance!


Solution

  • This should do it:

    cidr <- getwd()
    mkfldr <- "test1/project/code/example"
    dir.create(file.path(cidr, mkfldr), recursive = TRUE)