2

I've been looking in SO for a thread about how to create two (or n) subdirectories in a directory at once in R and only found this which it's not quiete what I want.

I can do it in two lines by doing:

dir.create(file.path(getwd(), "test"))

sapply(letters[1:2], 
   function(x) dir.create(file.path(getwd(), "test", paste0(x, "_test"))))

#   a    b 
#TRUE TRUE 

How can I do it in one line?

Thank you.

patL
  • 2,259
  • 1
  • 17
  • 38
  • 2
    what do yo mean by one line? what's wrong with your approach? – YOLO Jun 26 '18 at 13:59
  • With "one line" I meant at once, without do the first `dir.create` – patL Jun 26 '18 at 14:21
  • so you want to create sub directory without creating the parent directory? – YOLO Jun 26 '18 at 14:31
  • Sorry, maybe my comment is ambiguous. I want to crate a directory `test` and then create inside it two subdirectory `a_test` and `b_test`. But I'd rather do it at once, instead of do it in two steps. – patL Jun 26 '18 at 14:40

2 Answers2

6

dir.create(file.path(getwd(), 'test'), recursive = TRUE)

0
new_folder_path<-paste(getwd(),"new_folder",sep = "/")      
create_sub_folders<-function(x){
      dir.create(x)
      setwd(x)
      ifelse(dir.exists(x)!=TRUE,print("dir already exists"),dir.create(paste(x,"test_a",sep = "/")))
      ifelse(dir.exists(x)!=TRUE,print("dir already exists"),dir.create(paste(x,"test_b",sep = "/")))}

not sure if this is what you want. create the new_folder_path you want then just type this into the create_sub_folders function and it will create the subfolders you want..(sorry not in one line!)

e.matt
  • 836
  • 1
  • 5
  • 12