Announcement

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

  • shell path problems

    I need to run a command through PowerShell (through Stata). The command works fine as when I open shell through Stata and then copy and paste the command in, it works. However if I run the command from Stata with shell at the beginning it gives me a strange error.

    The code I run:

    Code:
    shell  powershell.exe -noexit -command " dir 'C:\Documents\Newfolder' -Filter '*.txt' | ForEach-Object { (Get-Content $_.FullName) | ForEach-Object { $_ -replace '""', '' } | Set-Content $_.FullName }"
    The error I get:

    Code:
    Get-Content : Cannot find path
    'C:\Documents\Newfolder\.FullName' because it does
    not exist.
    At line:1 char:90
    + ... Each-Object { (Get-Content .FullName) | ForEach-Object {  -replace '"',
    '' } | S ...
    +                    ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\Documents...older\.FullNa
       me:String) [Get-Content], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo
       ntentCommand

    I'm not sure if this is a Stata question as the error comes from PowerShell, but I was thinking maybe the Stata shell command is the culprit.

  • #2
    That's a Powershell issue, not a Stata issue (except in the sense that Stata is issuing an improper command to Powershell). If I saw the rest of your Stata syntax, might be able to find where it's going wrong.

    Two things come to mind that I find helpful when trying to troubleshoot situations like this.

    First is "display" the command to make sure it's properly structured for Powershell or whatever external program.

    Second, I'll build a macro or full-on variable to contain the contents of my external command. Then, it's as simple as executing the macro or variable into the shell.

    Oh, and a third approach: I have used outfile to generate .bat or .vbs files, then call the .bat or .vbs from Stata. Another possible approach, with another way to double-check that your Powershell syntax is appropriate and well-formed.

    With -preserve- and -restore- it's not too bad to generate a dataset that happens to have one variable, one case, that happens to contain the exact syntax you want to feed to the OS.

    No true solution, but hope these thoughts are useful in troubleshooting and getting to a true solution. .

    Comment


    • #3
      Doing shell bat worked in way. I cannot figure out how to make the bat inside Stata as when I try to make a local macro of the command
      Code:
       
       powershell.exe -noexit -command " dir 'C:\Documents\Newfolder' -Filter '*.txt' | ForEach-Object { (Get-Content $_.FullName) | ForEach-Object { $_ -replace '""', '' } | Set-Content $_.FullName }"
      , Stata gives the error "options not allowed". I've tried different enclosures like `"`...'"'
      Last edited by Ash Williams; 28 Dec 2014, 13:47.

      Comment

      Working...
      X