CreateDirectories.R

From Organic Design

<R> dir.exists <- function(x, curdir = ".", rootdir) {

  1. -------------------------------------------------------------------------------------- #
  2. Author : Marcus Davy
  3. This is a recursive program to take in a string such as:
  4. foo/fodda/fi, and create subdirectories foo, fodda, and fi
  5. from the current directory location (if they don't already exist)
  1. Note: It does the same work as the function dir.create in the base package, e.g.
  2. dir.create("foo/fodda/fi", recursive=TRUE)
  3. The recursive option is like Unix's 'mkdir -p foo/fodda/fi'.
  4. -------------------------------------------------------------------------------------- #
 if( missing(rootdir)) {
   rootdir <- getwd()
 }
 setwd(curdir)
 
 x <- unlist(strsplit(x,"/"))
 if( !file.exists(x[1]) ){
   dir.create(x[1])
   print(paste("Creating", file.path(getwd(), x[1])))
 }
 curdir <- x[1]
 if(length(x)==1) {
   setwd(rootdir)
 } else {
   return(dir.exists(x[-1], curdir = curdir,  rootdir = rootdir))
 }

} </R>