Announcement

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

  • Shell, Slash, and Local Macros

    I have some code with a simplified example below. I want to run a Windows shell command through a bunch of files. The problem seems to be that the backwards slash interacting with the local macro. I read this article by Nick Cox, http://www.stata-journal.com/sjpdf.h...iclenum=pr0042 and it detailed that Stata smartly deals with the problem. However since I'm exporting the command to shell that doesn't work here.

    I know of 2 solutions to this problem but wanted to know if there was a different solution that did not require extra macros or moving stuff.

    PROBLEM:

    Code:
    foreach i in a{
        forval k=2/2{
           shell pdftohtml.exe pdfs\`i'_`k'.pdf  htmls\`i'_`k'.html
        }
    }
    SOLUTION 1:

    Code:
    foreach i in a{
        forval k=2/2{
            local x= "htmls\"
            local z="pdfs\"
            shell pdftohtml.exe "`z'\"`i'_`k'.pdf  "`x'\"`i'_`k'.html
        }
    }
    SOLUTION 2:
    I can just do the command in my current folder and then move all the files with Stata's copy command.

    Thanks
    Last edited by Jack Stiles; 12 Apr 2015, 12:18.

  • #2
    3) If you are on Windows, you are not restricted to using backslashes \, you can also use normal slashes / , that won't trigger the macro `' problem

    4) You can replace the backslash with the ascii character and evaluate it: `=char(92)'

    Cheers,
    S

    Comment


    • #3
      3) If you are on Windows, you are not restricted to using backslashes \, you can also use normal slashes / , that won't trigger the macro `' problem
      Sergio Correia , no.

      In Stata on Windows the slashes are equivalent, but once you are starting calling programs outside of Stata, they are not as smart (or not as relaxed, depending on the point of view) as Stata, and different slashes mean different things, compare for example:
      dir /w
      vs
      dir \w

      (note that both produce identical results when run in Stata, but different results when run in command line window of Windows).

      This slash \ (whichever way is backward or forward) is used as an escape character to prevent macro expansion. Jack wants to pass the slash and evaluated macro to shell. Jack needs to duplicate the slash (escape from escape): \\ will be passed as one single slash \ to shell and the macro will be expanded by Stata.

      Best, Sergiy Radyakin

      Comment


      • #4
        Sergiy Radyakin Thanks for the solution. That is so much more simple than my approach.

        Comment

        Working...
        X