Announcement

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

  • Working with macros to change directory

    Dear all,

    I meet difficulties while changing the directory using macros.

    Specifically, I am trying to change to "F:/Research/economy"

    The word "economy" is stored in a local, because I loop over different folders in "F:/Research/"

    While running

    global path "F:\Research"

    foreach topic in "economy" {
    cd "$path`topic'"
    }

    I receive a weird message

    unable to change to F:\Research`topic'
    r(170);



    But while running

    global path "F:\Research"

    foreach topic in "economy" {
    cd "$path `topic'"
    }

    I receive

    unable to change to F:\Research\ economy
    r(170);

    which is of course true because there is no such folder " economy" (with a space before economy).

    What should I do?
    Last edited by Pavel Jelnov; 16 Sep 2016, 03:27.

  • #2
    Try adding a backslash for the subfolder names (it seems it gets trimmed off):

    Code:
    global path "F:\Research"
    
    foreach topic in \economy {
    cd "$path`topic'"
    }
    [As I see the backslash escapes the ` character]
    Last edited by Kazi Bacsi; 16 Sep 2016, 03:46.

    Comment


    • #3
      Nothing in your code supplies the slash you need, so the problem is inevitable.

      Code:
      global path "F:\Research"
      
      foreach topic in "economy" {
          cd "$path`topic'"
      }
      I disagree with Kazi's advice. Using backslash is a documented source of difficulties. See e.g..

      [U] 18.3.11 Constructing Windows filenames using macros http://www.stata.com/manuals14/u18.pdf

      http://www.stata-journal.com/sjpdf.h...iclenum=pr0042

      Forward slashes will work fine, even under Windows:

      Code:
      global path "F:/Research"
      
      foreach topic in "economy" {
          cd "$path/`topic'"
      }

      Comment


      • #4
        I just upgraded from Stata 11 IC to Stata 15 MP and encounter "unable to change" directory error in Stata 15 that I don't encounter in Stata 11, when trying to change directory using a local macro.

        Code:
        display %td_NN_CCYY date(c(current_date), "DMY")
        07 2017

        .
        . local mesyr : di %td_NN_CCYY date(c(current_date), "DMY")

        . di "`mesyr'"
        07 2017

        cd "C:\Bossa Nova\collections agency models\rbrasilsolucoes\IBI"
        C:\Bossa Nova\collections agency models\rbrasilsolucoes\IBI

        . local drive "`mesyr'"

        . cd "`drive'"
        unable to change to 07 2017


        If I manually input the directory name with cd command, it works. It also works fine with local macro in Stata 11.
        What change do I need to make in this code for it two work in Stata 15?

        Thanks.

        Comment

        Working...
        X