Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use local macro and mkdir to create multiple folders

    Hi all,

    I'm trying to run the following code to create a loop of creating multiple folders at once.

    This is an example

    Code:
     
    
    local dir "C:\Users\"
    local d "AA AB"
    foreach x in `d'{
    mkdir "C:\Users/`d'"
    }
    This ended up creating one folder named "AA AB", instead of two folders. (I wanted to create two folders, "AA" and "AB")

    Any help is appreciated! Thanks!

  • #2

    The issue is Stata sees "AA BB" as a single token/item in the macro list. You can either add quotes (or better double quotes ) around your local folder names manually (e.g., "AA" "BB") or use a macro extended function like:
    Code:
    loc d : list retokenize d
     
    foreach x in `d' {
    di `"`x'"'
    cap  mkdir `"`dir'`x'"'  //using capture lets you rerun in a dofile and skip this part if the folder exists ... also you can use your local 'dir' here
    }
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      eric_a_booth Thank you for the reply! I tried both ways, but they both didn't seem to work. Not sure why.

      Comment


      • #4
        Originally posted by Peter Li View Post
        eric_a_booth Thank you for the reply! I tried both ways, but they both didn't seem to work. Not sure why.
        You'd have to provide the code you tried in order to help other assist you in tracing the error. Your first example above is a general example but there might be something missing from it that you have in your actual code. Two guesses (1) you probably need a slash (/) between `dir' and `x' in the path to differentiate between the macros and (2) you need to check how Stata is tokenizing your list (do your items "AA" have spaces in them like "Folder One" and is the list that's getting passed to the loop keep/retain the quotes (you can examine what's in the macro with -display- if not, you might just need the outer double quotes around the macro (like I've got for 'local d' below) ).

        Both of these 2 examples work but if your examples don't look like this, I would need more details to help:



        Code:
        local dir "`c(pwd)'"
        local d `" "AA" "AB" "CC" "DD" "EE" "'
        
          
        foreach x in `d' {
        di `"`x'"'
        cap  mkdir `"`dir'/`x'"'   
        }
        
        dir
        
        
        
        
        
        
        local dir "`c(pwd)'"
        
        loc d : list retokenize d
        local d  " e f g h i j k "
        
        di `"`d'"'
          
        foreach x in `d' {
        di `"`x'"'
        cap  mkdir `"`dir'/`x'"'   
        }
        
        dir
        Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

        Comment


        • #5
          eric_a_booth Hi Eric, the 2nd method works for me now! the missing "/" appeared to be the reason why it didn't work the first time.

          I really appreciate your help

          Comment

          Working...
          X