Announcement

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

  • Evaluating macros containing ' in Mata

    Hi
    I just made a post with what I thought was a Stata problem,
    But for me is a Mata problem.

    I would like to get files and directories in a root directory from Mata.

    the following code works in most cases, but not when there is a ' in the root directory name, eg:
    Code:
     mata fls = sprintf(`"\`"\`: dir %s files *'"'"', sprintf(`""%s""', "c:/nhb's folder"))
    . mata fls
      `"`: dir "c:/nhb's folder" files *'"'
    . mata st_macroexpand(fls)
    invalid syntax
      `"s folder" files *'"'
    The ' in the directory name makes the code evaluate `: dir "c:/nhb' which is an empty string and gives an error.
    Then `"s folder" files *'"' is printed.

    Do anyone know a workaround for this?

    Thank you in advance.
    Kind regards

    nhb

  • #2
    Any reason not to directly use Mata's dir() function to get the files and directories? The only thing that is not supported, as far as I can see, is the respectcase option that could be relevant under Windows.

    Best
    Daniel

    Comment


    • #3
      Hi Daniel
      My rescuer for the second time today.
      Thank you very much.
      I totally overlooked that function.
      A bit embarrassing

      Once again thank you

      Regards/nhb
      Kind regards

      nhb

      Comment


      • #4
        See also pathjoin() for functions manipulating pathnames.

        Comment


        • #5
          Thank you. I will
          Kind regards

          nhb

          Comment

          Working...
          X