Announcement

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

  • Check if directory exists before running mkdir

    I'm translating some code between R and Stata with Stata 14 with Linux OS.
    I'm pretty good at R and C programming, but Stata has me flummoxed. I would say the Stata user protection features have defeated me, actually.

    All my scripts create directories. Stata for that is

    Code:
    mkdir "../workingdata"
    That works once, but the second time this Stata program runs, it fails (and stops there!) because the directory already exists. I'd like to turn off the "stop there" feature in general and rewrite this to be smarter also. If the script would just keep running, I would be happy.

    In Stata I can't find how to check if a directory exists. This seems like an obvious thing. In R, I'd write
    Code:
    wdir <- "../workingdata/"
    if (!file.exists("../workingdata/")) dir.create(wdir)
    In Stata help, I find a function "direxists", but if I try to use it, its a fail:

    Code:
    .direxists("../workingdata")
    command direxists is unrecognized
    Obviously, I don't know how to use that one.

    Generally, I'd like this script to keep running, even if Stata disagrees. But I've read the history in your forum and its a pretty unpopular suggestion

    pj

  • #2
    direxists() is a Mata function. Even if it were a Stata function, you couldn't call it like that, as functions require a command context.

    Compare this example. Know that when and where I applied it a directory c:\data did exist and a directory c:\frog did not exist.

    Code:
    . mata : st_numscalar("OK", direxists("c:\data"))
    
    . di scalar(OK)
    1
    
    . mata : st_numscalar("OK", direxists("c:\frog"))
    
    . di scalar(OK)
    0
    Otherwuse see this thread started earlier today: http://www.statalist.org/forums/foru...irmdir-command


    Last edited by Nick Cox; 06 Jun 2016, 15:40.

    Comment


    • #3
      Personally, I just capture the return code and ignore the error:

      Code:
      capture mkdir "../workingdata"

      Comment


      • #4
        If you want to be precise

        Code:
        capture confirm file "../workingdata/"
        if _rc mkdir "../workingdata/"
        Alfonso Sanchez-Penalver

        Comment


        • #5
          Alfonso: Checking for a file is not the same as checking for a folder or directory.

          Comment


          • #6
            Nick: I have used this and it works. I know a file it's not a directory, but it works. You can check it out
            Code:
            capture confirm file "/"
            di _rc
            capture confirm file "/doodoo/"
            di _rc
            Checking the root directory gives an _rc value of 0, whereas when checking for the directory doodoo it gives a value of 601. That is if you don't have the doodoo directory. :D
            Alfonso Sanchez-Penalver

            Comment


            • #7
              What happens if you have file frog but want to check for directory frog? If Stata treats the two together, how can it reliably treat the two apart?

              Comment


              • #8
                You still have to specify the path to the file or the directory. Let's say that you have the file frog.txt in the directory frog that is in the root. The directory check would be
                Code:
                capture confirm file "/frog/"
                The file check
                Code:
                capture confirm file "/frog/frog.txt"
                If you had the file frog.txt in the root
                Code:
                capture confirm file "/frog.txt"

                I don't see why Stata would treat them both together. Each time you're pointing to a specific point.
                Alfonso Sanchez-Penalver

                Comment


                • #9
                  Indeed, in practice a file name and a directory name would not usually be identical. But there's no rule that a filename must include an extension so far as I know.

                  Some of the time I program, and I want my programs to work. That's why I press the point. I don't like "should work", "mostly works". I prefer "always works".

                  That's why I prefer either Robert's code or mine to this.

                  You're right. It's hard to imagine right now that your suggestion wouldn't work for most cases, but I submit that that is not bullet-proof.

                  Comment


                  • #10
                    Nick,

                    I really don't see when you would have a file without an extension. The problem would come when checking for the file, though, not the directory as long as you include the slash after the name: "/frog/" points to a directory, not a file. I have nothing against either your solution or Robert's. But my solution works for checking directories, which is what was asked in this question. It's not that it should work, or mostly works. Add the slash and it always works.
                    Alfonso Sanchez-Penalver

                    Comment


                    • #11
                      That's a helpful resolution. The slash is indeed needed to disambiguate between files without an extension and folders with similar names.

                      Comment


                      • #12
                        Five years late on this response, but I have accomplished this by capturing the return code with cd. I prefer using shell with mkdir as it will create multiple level subdirectories whereas Stata's mkdir will throw an error parent directory one level up does not already exist

                        Code:
                        capture cd "..../workingdata"
                        if _rc!=0 {
                        shell mkdir "..../workingdata"
                        }

                        Comment

                        Working...
                        X