Announcement

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

  • *mkdir* can only create one directory at a time

    Error will occur if a parent folder does not exist for the folder that I would like to create.

    mkdir C:\project\temp
    could not create directory C:\project\temp

    This is not well documented in the help file though.

    To solve this problem, just create directories step-by-step:

    mkdir c:\project
    mkdir c:\project\temp

  • #2
    I've bumped into this myself before, and agree that at least an option would be convenient. Mata offers functions that allow you to automate this. Below is one attempt. It creates the directory stored in variable dir along with any nonexistent parent directories.

    Code:
    mata:
    
    dir = "C:\sandbox\x\y\z"
    
    mk = J(1, 0, "")
    base = ""
    while (!direxists(dir)) {
        pathsplit(dir, dir, base)
        mk = base, mk
    }
    n = length(mk)
    for (j = 1; j <= n; j++)
        mkdir(dir = pathjoin(dir, mk[j]))
    
    end

    Comment


    • #3
      It's a limitation on the OS as much or more than a limitation on Stata. I suppose they could have had the command in Stata parse things and make multiple calls to the OS like in the Mata example, but if the OS can't do it normally, I see no strong reason to make Stata go beyond the OS.

      Comment


      • #4
        In a Windows machine, you can use -shell mkdir- instead of Stata's mkdir, to call the OS's mkdir function, which creates folder trees by default.

        In an Unix machine, use -shell mkdir -p -
        Jorge Eduardo Pérez Pérez
        www.jorgeperezperez.com

        Comment

        Working...
        X