Announcement

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

  • shell command - not working if initiated from Stata but working otherwise

    Dear Statalist

    I run the following code using Stata14:

    !rm "C:/Users/user/Desktop/Q1.pdf"

    It does not remove the file.

    But running the following from Windows Power Shell removes the file:
    rm "C:/Users/user/Desktop/Q1.pdf"

    Any advice on how to get the Stata shell command works? I thought it would just work by putting the code you use in the shell into stata and put a "!" before the code and then you can get the result as if the code is run from the shell direct. But it seems this is not the situation. Grateful for any help!

    Thanks
    Charlie.





  • #2
    I don't think the common or garden shell understands forward slashes as you wish and perhaps not even -rm-. I recall -del- as the command to use here.

    Comment


    • #3
      Besides Nick Cox' notoriously correct answer, you can erase your file from within Stata without escaping to your operating system's shell.

      If you're working on a Windows operating system (as your remark on using Windows PowerShell implies), the correct shell-escaping command would in Stata would be
      Code:
      !DEL "C:\Users\user\Desktop\Q1.pdf"
      or
      Code:
      !ERASE "C:\Users\user\Desktop\Q1.pdf"
      , as Nick pointed out,

      However, there is no need to escape to the Windows shell in the first place, as Stata features an "erase" command (which can also be used by typing "rm") directly (and it will do the file-path substitution of slashes for you). You can skip the hassles of finding the correct shell command by directly removing your file from within Stata:
      Code:
      erase "C:/Users/user/Desktop/Q1.pdf"
      Regards
      Bela
      Last edited by Daniel Bela; 12 Sep 2017, 05:49. Reason: typo

      Comment


      • #4
        Originally posted by Daniel Bela View Post
        Besides Nick Cox' notoriously correct answer, you can erase your file from within Stata without escaping to your operating system's shell.

        If you're working on a Windows operating system (as your remark on using Windows PowerShell implies), the correct shell-escaping command would in Stata would be
        Code:
        !DEL "C:\Users\user\Desktop\Q1.pdf"
        or
        Code:
        !ERASE "C:\Users\user\Desktop\Q1.pdf"
        , as Nick pointed out,

        However, there is no need to escape to the Windows shell in the first place, as Stata features an "erase" command (which can also be used by typing "rm") directly (and it will do the file-path substitution of slashes for you). You can skip the hassles of finding the correct shell command by directly removing your file from within Stata:
        Code:
        erase "C:/Users/user/Desktop/Q1.pdf"
        Regards
        Bela
        Thank you very much Bela, and Nick. So I presume the logic is that when we run "! SOMETHING" in Stata, it is not equivalent to running "SOMETHING" directly from the OS shell - only a subset of SOMETHING works.

        A separate but related question. I was actually playing with "pdftk" to combine multiple files. The following code works well in Stata:

        !pdftk "C:\Users\user\Q1.pdf" "C:\Users\user\Q2.pdf" cat output "C:\Users\user\Desktop\Q.pdf"

        But is there a way to declare the directory so that I don't need to repeat "C:\Users\user" ?

        P.S. apart from the brief documentation (https://www.stata.com/manuals13/dshell.pdf) on Stata's shell command, is there any more reference? For example, I don't know where to find the reference on putting " /s /q " at the end of "!rmdir "`f'_files" /s /q".

        Grateful for any guidance!

        Regards
        Charlie

        Comment


        • #5
          I expect that ! lets me run shell commands. Your problem is that what you were passing to the shell is evidently illegal.

          I don't expect Stata to document what you can do in the Windows shell. Given a OS (MS-DOS) command prompt something like

          rmdir /?

          fires up syntax details.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            I expect that ! lets me run shell commands. Your problem is that what you were passing to the shell is evidently illegal.

            I don't expect Stata to document what you can do in the Windows shell. Given a OS (MS-DOS) command prompt something like

            rmdir /?

            fires up syntax details.
            To illustrate my first query, the following two lines both work in my Windows Power Shell:

            rm "C:\Users\user\Desktop\Q1.pdf"

            del "C:\Users\user\Desktop\Q1.pdf"

            but if run from Stata, only the second line works, and I tried both forward and backward slashes.

            As to rmdir, I made reference to https://www.stata.com/statalist/arch.../msg00064.html. I am not sure whether the " /s /q" part falls under Stata or the shell.

            Grateful for advice.

            Charlie

            Comment


            • #7
              Windows Power Shell is a downloadable extra, which evidently you have installed on your machine. It's not the shell strict sense, and so it's not what the shell command in Stata accesses. Why do you think that Stata would know (a) that you want it accessed (b) how to call it up? There may be a way to call it directly from Stata but I can't advise on that.

              rm is not part of the default set of Windows commands as accessible via the command prompt: as Daniel Bela and I have posted already del is the standard equivalent.

              There is no cause for uncertainty about the syntax of rmdir as

              1. help rmdir in Stata documents no such switches

              2. Presumably your Power Shell is documented somewhere for you to find out what it supports (if not, why take it seriously?)

              Comment


              • #8
                Thanks Nick. I didn't know there are different shells in the Windows system so I assumed stata called PowerShell up when I run the ! command. Apparently I was wrong.

                Comment

                Working...
                X