Announcement

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

  • Directory path: Folder with folders

    Dear All,

    this is probably a really simple thing, and has probably been asked before and I'm not using the right words when searching for it.

    Is it possible to set a directory path to a folder which contains more folders, and then use a file in one of those folders in the folder without spelling out the entire path to start using it? I use Stata on a mac, and just referring to the file's name doesn't seem to work.

    Thank you in advance!

    Best regards,
    Marco

  • #2
    Maybe I misunderstand the question, but my guess is all you need to achieve this is the command -cd-; assuming you want to open a file called "MAINDIRECTORY/SUBDIRECTORY/FILE.dta", this would work:
    Code:
    cd "MAINDIRECTORY"
    use "SUBDIRECTORY/FILE.dta"
    Regards
    Bela

    Comment


    • #3
      Daniel Bela
      Hi Daniel,

      thank you,

      there, "SUBDIRECTORY/FILE.dta" would simply be the part of the entire directory that isn't already included in MAINDIRECTORY, as in I can just delete MAINDIRECTORY from the entire path and leave the rest, right? That doesn't seem to work.

      Regards,
      Marco

      Comment


      • #4
        Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

        Section 12.1 is particularly pertinent

        12.1 What to say about your commands and your problem

        Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
        ...
        Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.

        Comment


        • #5
          You just need to set the working directory to the folder you want to use (in this case MAINDIRECTORY). Then you simply type:

          Code:
          use "./SUBDIRECTORY/file.dta"
          Last edited by Belinda Foster; 29 May 2017, 06:47.

          Comment


          • #6
            To specifiy what I did:

            Code:
            cd "/Users/marcogallo/Documents/FolderX"
            use "/Subfolder1/document.dta", clear
            where

            Code:
            use "/Users/marcogallo/Documents/FolderX/Subfolder1/document.dta", clear
            opens the desired file.

            I also tried to leave out the first slash:

            Code:
            use "Subfolder1/document.dta", clear

            Comment


            • #7
              Thank you Belinda, the "." (dot) at the beginning of the subdirectory is what I was missing!

              Comment


              • #8
                Well what i suggested works interactively. Alternatively, you can save the path to a local and then use it in the do file:

                Code:
                local marcosdir "/Users/marcogallo/Documents/FolderX/"
                use "`marcosdir'/Subfolder1/document.dta"

                Comment


                • #9
                  Another remark: It is not the missing dot "." that messed up Marco Gallo's code, but the leading slash "/" in the file path.

                  For summing this issue up, I wrote a quick minimal example to show the differences (to be executed in one run as a do-file, not interactively or line-by-line, otherwise it will leave behind temporary stuff!):
                  Code:
                  clear
                  pwd // this displays our current working directory
                  
                  * create test setup
                  mkdir testsubdir
                  mkdir testsubdir/anothersubdir
                  quietly : sysuse auto
                  save "testsubdir/anothersubdir/auto.dta"
                  
                  /* here starts the the interesting part */,
                  
                  * use file without descending to "testsubdir"
                  capture : noisily use "testsubdir/anothersubdir/auto.dta" // works (no "./")
                  capture : noisily use "./testsubdir/anothersubdir/auto.dta" // works (with "./")
                  capture : noisily use "/testsubdir/anothersubdir/auto.dta" // does NOT work
                  
                  * use file after descending to "testsubdir"
                  cd testsubdir
                  capture : noisily use "anothersubdir/auto.dta" // works (no "./")
                  capture : noisily use "./anothersubdir/auto.dta" // works (with "./")
                  capture : noisily use "/anothersubdir/auto.dta" // does NOT work
                  
                  /* end of the interesting part */
                  
                  * erase test setup file and subdirectories
                  rm "./anothersubdir/auto.dta"
                  rmdir "./anothersubdir"
                  cd ..
                  rmdir "./testsubdir"
                  What I want to stress is: It is completely equivalent to write "./SUBDIRECTORY/FILE.dta" (with "./") and "SUBDIRECTORY/FILE.dta" (without "./"); writing a file path with a leading slash "/" like "/SUBDIRECTORY/FILE.dta" is necessarily something different, as it refers to an absolute (as opposed to a relative) file path.

                  Regards
                  Bela

                  Comment

                  Working...
                  X