Announcement

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

  • Bug in the dir extended macro


    Dear All,

    I wonder if anyone can interpret the below syntax line for me.

    Thank you, Sergiy

    Click image for larger version

Name:	help_macro_dir.png
Views:	1
Size:	8.3 KB
ID:	1699012





    Last edited by Sergiy Radyakin; 26 Jan 2023, 16:06.

  • #2
    Scroll down further in the output of help macro to get to the detailed description of each of the one-line descriptions of the commands. For the macro dir command it's fairly long, but here is a picture of the start.

    Click image for larger version

Name:	macro.png
Views:	1
Size:	732.5 KB
ID:	1699016

    Comment


    • #3
      Thank you William Lisowski . Specifically, what do square brackets denote in the syntax? Thank you, Sergiy

      Comment


      • #4
        Square brackets denote things that are optional. It might be more accurately said that they denote things that may be optional. For example, if the name of the directory (for dirname) has no embedded spaces, then the quotation marks can be omitted.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          ... For example, if ... has no embedded spaces, then the quotation marks can be omitted.
          This was my understanding, but I can't explain my observations with this:

          Code:
          cd "c:\data\test\"
          sysuse auto
          
          save "autotest.dta", replace
          outsheet using "autotest.txt", replace
          dir
          
          display `"`: dir . files *'"'
          display `"`: dir . files *.dta'"'
          display `"`: dir c:/data/test/ files *.dta'"'
          display `"`: dir c:\data\test\ files *.dta'"'
          display `"`: dir c:\data\test\ files "*.dta"'"'
          display `"`: dir "." files *.dta'"'
          display `"`: dir . files "*.dta"' "'
          display `"`: dir "." files "*.dta"' "'

          Code:
          . cd "c:\data\test\"
          c:\data\test
          
          . sysuse auto
          (1978 automobile data)
          
          .
          . save "autotest.dta", replace
          file autotest.dta saved
          
          . outsheet using "autotest.txt", replace
          
          . dir
            <dir>   1/27/23 15:55  .                 
            <dir>   1/27/23 15:55  ..                
            12.5k   1/27/23 15:55  autotest.dta      
             4.7k   1/27/23 15:55  autotest.txt      
          
          .
          . display `"`: dir . files *'"'
          "autotest.dta" "autotest.txt"
          
          . display `"`: dir . files *.dta'"'
          varlist not allowed
          
          
          . display `"`: dir c:/data/test/ files *.dta'"'
          invalid syntax
          
          
          . display `"`: dir c:\data\test\ files *.dta'"'
          invalid syntax
          
          
          . display `"`: dir c:\data\test\ files "*.dta"'"'
          invalid syntax
          
          
          . display `"`: dir "." files *.dta'"'
          varlist not allowed
          
          
          . display `"`: dir . files "*.dta"' "'
          "autotest.dta"
          
          . display `"`: dir "." files "*.dta"' "'
          "autotest.dta"

          A few years ago Nick Cox wrote:
          I don't like "should work", "mostly works". I prefer "always works".
          The documentation shown by William Lisowski mentions that "The quotes in the command are optional but recommended, and they are nearly always required surrounding pattern." Leaving the exact conditions for when they are required as a guesswork to the user. The examples I demonstrate illustrate that the intuitive rule of presence of a space is not working here.

          Since macro expansion is done as a built-in, it is not possible to trace it to determine clearly what it is is doing.

          Hence my question is to the developers:

          - is there a clear specification of when the quotes are a requirement in the dir macro?
          - or is this a bug?
          - or is this an inaccuracy of the documentation?


          Thank you, Sergiy

          PS: I recall getting into the same error multiple times, so will not be surprised if the same question was already posted elsewhere, but it's just hard to formulate a search query to seek across the whole internet or even this forum for "files AND dir AND bug" - the results are just too numerous and irrelevant.

          Comment


          • #6
            I can confirm that I get the same results as Sergiy.

            The documentation quite clearly indicates that the quotes are optional. Even factoring in the implicit understanding that any string must be wrapped in quotes if it contains embedded blanks, the command does not perform to specification when given input that conforms to its requirements. By definition, that's either a bug or a documentation error.

            It's interesting that I have never stumbled on this myself. I routinely enclose the directory name and pattern in quotes when I use this local macro command (which is pretty often). I wonder if in older versions' documentation the quotes were shown as required, and I just never noticed the change in documentation and maintained the habit.

            Comment


            • #7
              I believe the problem is not one of quoting - although quoting will solve the problem - but rather that the macro dir command interprets almost some unquoted strings containing "*" or "-" as a variable name including wildcards, on a command where it has no business looking for variable names.
              Code:
              . dir
              
              total 48
              -rw-r--r--  1 lisowskiw  staff      0 Jan 27 18:47 autotest-all
              -rw-r--r--  1 lisowskiw  staff  12765 Jan 27 18:42 autotest.dta
              -rw-r--r--  1 lisowskiw  staff   4761 Jan 27 18:42 autotest.txt
              
              . local m1 : dir . files *
              
              . macro list _m1
              _m1:            "autotest-all" "autotest.txt" "autotest.dta"
              
              . local m2 : dir . files *.dta
              varlist not allowed
              r(101);
              
              . local m3 : dir . files "*.dta"
              
              . macro list _m3
              _m3:            "autotest.dta"
              
              . local m4 : dir . files autotest-all
              varlist not allowed
              r(101);
              
              . local m5 : dir . files "autotest-all"
              
              . macro list _m5
              _m5:            "autotest-all"
              
              .

              Comment

              Working...
              X