Announcement

Collapse
No announcement yet.

Shell problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Shell problem

    I have a command line
    Code:
    tasklist /FI "IMAGENAME eq notepad.exe"
    I want to shell that and redirect it to Running.txt.

    The following prints to the console but I am having a problem redirecting it. I have tried a few variations son a theme, but I am getting nowhere. Any idesa?
    Code:
    #Break On
    #Compile exe
    #Dim All
    
    Function PBMain
    Local temp As String
      temp = "IMAGENAME eq notepad.exe"
      Shell "tasklist /FI " + $DQ + temp + $DQ
      Waitkey$
    End Function

  • #2
    David

    Try
    Code:
    Shell "tasklist /FI " + $DQ + temp + $DQ + " > Running.txt"

    Comment


    • #3
      Hi Ribeiro

      I have tried that and one of the many that I did.

      Your suggestion gives:
      Code:
      ERROR: Invalid argument/option - '>'.
      Type "TASKLIST" /? for usage.
      So '>' is being pulled into the Tasklist command line but it shouldn't.

      Added: I should mention that your suggestion works in FreeBASIC, I tried it earlier, but I have learned that Windows now allows filenames in quotes within command lines removing the necessity to use $DQ.

      Who would have thought that this would work but it does.
      Code:
      Shell "tasklist /FI ""IMAGENAME eq notepad.exe"""
      However, I still cannot get it to redirect.

      Comment


      • #4
        David,

        Have you tried building the entire string in advance of the SHELL statement, then just do: SHELL sCommandLine

        I've hit this before, so let me search and see if I can find something specific for you..

        -John

        Found a couple of examples:
        Code:
         Shell ("cmd /C dir " & gsSearchFolder & "\*.bas  > xxx.txt /s", 0)
         Open "xxx.txt" For Binary As #1 : Get$ #1, Lof(1), temp$ : Close #1
        Code:
           Shell Environ$("Comspec") + " /C " + "NetStat -b > ./stat.txt", 0
        Code:
         Local lExitCode As Long
         Local m As String
        
         If IsFile("tempfile.txt") Then Kill "tempfile.txt"
        
         'Shell "NETSTAT -n | find " & $DQ & ":3389" & $Dq & " | find " & $Dq & "ESTABLISHED" & $Dq, exit to lExitCode
         'm = "||" & "NETSTAT -n | find " & $DQ & ":3389" & $Dq & " | find " & $Dq & "ESTABLISHED" & $Dq & "||"
         m = $Dq & "NETSTAT -n | find " & $Dq & ":3389" & $Dq & " | find " & $Dq & "ESTABLISHED" & $Dq & " > tempfile.txt" & $Dq
        
         m = "NETSTAT -n | find " & $Dq & $Dq & $Dq & ":1370" & $Dq & $Dq & $Dq & " > tempfile.txt"
         m = "NETSTAT -n "
        
         'm =         "NETSTAT -n | find " & $DQ & ":3389" & $Dq & " | find " & $Dq & "ESTABLISHED" & $Dq & " > tempfile.txt"
         'm =         "NETSTAT -n > tempfile.txt"      '  verify redirection works
         ? m
         Shell $Dq & m & $Dq, Exit To lExitCode
         ? "lExitCode = " & Trim$(lExitCode)

        Comment


        • #5
          John is correct.

          As per PBHelp..
          To use internal DOS commands like DIR and COPY, you must run the DOS command processor, passing the DOS command as a parameter.

          ie
          Code:
          #Break On
          #Compile exe
          #Dim All
          
          Function PBMain
            Local CommandSpec, temp As String
          
            temp = "IMAGENAME eq notepad.exe"
            CommandSpec = Environ$("comspec") ' command interpreter rqd for DOS Commands like ">"
            Shell CommandSpec + " /C " + "tasklist /FI " + $DQ + temp + $DQ + "> Running.txt"
            Waitkey$
          End Function
          Rgds, Dave

          Comment


          • #6
            David

            This works although it is not exactly what you want.

            Code:
            #BREAK ON
            #COMPILE EXE
            #DIM ALL
            
            FUNCTION PBMAIN
              SHELL ("cmd /C TASKLIST > Running.txt" )
              PRINT "Done"
              WAITKEY$
            END FUNCTION

            Comment


            • #7
              I usually write a batch file with PB then run it with shell, shell function or CreateProcess. You delete it after it has run.
              hutch at movsd dot com
              The MASM Forum

              www.masm32.com

              Comment


              • #8
                Thanks, Dave, that is perfect for my needs.

                Comment


                • #9
                  Originally posted by Dave Biggs View Post
                  John is correct.

                  As per PBHelp..
                  [COLOR=#000000]To use internal DOS commands like DIR and COPY, you must run the DOS command processor, passing the DOS command as a parameter.
                  But TASKLIST is not an internal DOS command, it is a .exe file. (in the SYSTEM directory) so there is no need for "comspec /c"

                  Correction: replace third (highlghted) "is" with "should be" (see next post!)

                  Comment


                  • #10
                    This is strange. It turns out you do need to call a command processor despite the fact that TASKLIST is a.exe, not an internal command

                    Code:
                    #COMPILE EXE
                    #DIM ALL
                    FUNCTION PBMAIN () AS LONG
                        LOCAL strS,StrT,StrR AS STRING
                        strS = "tasklist /FI ""IMAGENAME eq firefox.exe"" >test1.txt"
                        SHELL strS
                        strS = "tasklist /FI ""IMAGENAME eq firefox.exe"" >test2.txt"
                        SHELL ENVIRON$("COMSPEC") + " /C " & strS
                        OPEN "test1.txt" FOR INPUT AS #1
                        WHILE NOT EOF(1)
                            LINE INPUT#1 ,strT
                            strR += strT
                        WEND
                        CLOSE #1
                        ? strR
                        strR = ""
                        OPEN "test2.txt" FOR INPUT AS #1
                        WHILE NOT EOF(1)
                            LINE INPUT#1 ,strT
                            strR += strT
                        WEND
                        CLOSE #1
                        ? strR
                    
                    END FUNCTION
                    Dare I say the word "bug"? SHELL certainly doesn't work as documented.

                    Edit later:
                    No I dare not. As Dave Briggs correctly points out below the output redirection ">" is an "internal command" so the ENVIRON$("COMSPEC") + " /C " is required.
                    Last edited by Stuart McLachlan; 30 Aug 2019, 10:29 PM.

                    Comment


                    • #11
                      Hmmm, first version return an EXIT code of 1.


                      Code:
                      #COMPILE EXE
                      #DIM ALL
                      FUNCTION PBMAIN () AS LONG
                          LOCAL strS,StrT,StrR AS STRING
                          LOCAL dwN AS DWORD
                          strS = "tasklist /FI ""IMAGENAME eq firefox.exe"" >test1.txt"
                          SHELL strS EXIT TO dwN
                          ? STR$(dwN)
                          strS = "tasklist /FI ""IMAGENAME eq firefox.exe"" >test2.txt"
                          SHELL ENVIRON$("COMSPEC") + " /C " & strS EXIT TO dwN
                          ? STR$(dwN)
                          OPEN "test1.txt" FOR INPUT AS #1
                          WHILE NOT EOF(1)
                              LINE INPUT#1 ,strT
                              strR += strT
                          WEND
                          CLOSE #1
                          ? strR
                          strR = ""
                          OPEN "test2.txt" FOR INPUT AS #1
                          WHILE NOT EOF(1)
                              LINE INPUT#1 ,strT
                              strR += strT
                          WEND
                          CLOSE #1
                          ? strR
                      
                      END FUNCTION

                      Comment


                      • #12
                        Hey Stuart,

                        But TASKLIST is not an internal DOS command, it is a .exe file. (in the SYSTEM directory) so there is no need for "comspec /c"

                        . . .

                        This is strange. It turns out you do need to call a command processor despite the fact that TASKLIST is a.exe, not an internal command
                        Strange, yes. I think that over time, the internal workings of CMD drifted away from the specs of the original DOS Command Processor.

                        In the olden days of DOS, I had written some tools to show what "level" of command processor was in use, mostly because I needed the Environment Variables to be set properly all the time...
                        I learned that an EXE which is run from a command prompt "inherits" the parent's environment, and any changes made by that EXE would not persist, because its environment would be discarded after the EXE completed. So, I learned how to change a variable IN THE PARENT environment. (Had to detect the PSP, find the parent's PSP, etc...) Turns out that the parent environment was inherited from the "root", which could also be changed by following the PSP chain. It also made a difference when invoking a new command processor from a commandline -- and whether it was invoked with no options, the /C option, or the /K option... Has to do with which level of shell is being created and whether you want it to persist.

                        All that by way of explaining that my experience has taught me that I get most control over the command processor when I invoke it myself (via the "COMSPEC" setting), rather than allowing the SHELL statement to rely on whatever happens to be in the current environment.

                        So, regardless of whether the process I want to SHELL to is an internal Command Processor statement or an "external" EXE, I now invoke via COMSPEC and get more reliable runs and results. By now it's mostly habit and I don't have to make the distinction, nor worry about failure to interpret my commandline properly.

                        Just some unscientific musings that I hope are helpful, or at least interesting.

                        -John

                        Comment


                        • #13
                          But TASKLIST is not an internal DOS command, it is a .exe file. (in the SYSTEM directory) so there is no need for "comspec /c"
                          " > " is the internal command that prescribes comspec
                          Rgds, Dave

                          Comment


                          • #14
                            Originally posted by Dave Biggs View Post

                            " > " is the internal command that prescribes comspec
                            Ah yes, obscure but logical.

                            Comment

                            Working...
                            X