Announcement

Collapse
No announcement yet.

Help with "SHELL" statement

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

  • Help with "SHELL" statement

    Windows 10 Pro (64).

    When I run the following code :

    Code:
    LOCAL mycommand AS STRING
    
    mycommand = "C:\Program Files\7-Zip\7z.exe l C:\D_Drive\1-test.zip > C:\D_Drive\1-test.txt"
    SHELL mycommand, 0
    I get the error:

    Code:
    75: Path/file access error
    When I tried the following in the "Command Prompt" window:

    Code:
    C:\Program Files\7-Zip>7z l C:\D_Drive\1-test.zip > C:\D_Drive\1-test.txt
    it works perfectly.

    The only thing that comes to mind is : Can you SHELL to a 64-bit program from PowerBasic (which is 32-bit)?

  • #2
    Yes you can.

    IIRC you need quotes around any segment of the commandline string that contains a non-delimiting space...

    mycommand = $DQ + "C:\Program Files\7-Zip\7z.exe" + $DQ + " l C:\D_Drive\1-test.zip > C:\D_Drive\1-test.txt"
    "Not my circus, not my monkeys."

    Comment


    • #3
      To use internal DOS commands like the redirection operator '>' you must run the DOS command processor, passing the DOS command as a parameter. Example:

      pid??? = SHELL(ENVIRON$("COMSPEC") + " /C DIR *.* > filename.txt")
      Rgds, Dave

      Comment


      • #4
        Eric and Dave have identified different problems with your method. Follow both sets of adivce.

        Comment


        • #5
          I replaced the 64-bit version with the 32-bit version in "Program Files (x86)" and then the SHELL worked without errors but I haven't found out if it is possible to direct the
          list to a file as I have done in the Command window. The documentation isn't very helpful:

          Code:
          [B]l (List contents of archive) command[/B]
          
          
          Lists contents of archive.
          [B]Examples[/B]
          
          
          7z l archive.zip
          lists all files from archive archive.zip
          .

          I will go back to "Zlib" which works perfectly but I thought it would be worth using something more modern!

          Comment


          • #6
            Did you try this?

            LOCAL mycommand AS STRING
            mycommand = "C:\Program Files\7-Zip\7z.exe l C:\D_Drive\1-test.zip > C:\D_Drive\1-test.txt"

            SHELL (ENVIRON$("COMSPEC") + " /C " + mycommand, 0)
            Rgds, Dave

            Comment


            • #7
              Compilable version tested ok here.
              Code:
              #DIM All
              #COMPILE EXE
              #INCLUDE "WIN32API.INC"
               
              FUNCTION PBMAIN()
               LOCAL mycommand AS STRING
               
                mycommand = $DQ + "C:\Program Files\7-Zip\7z.exe" + $DQ + " l C:\test\test.zip > C:\test\a-test.txt"
                          ' $DQ + "C:\Program Files (x86)\7-Zip\7z.exe" + $DQ .. for 32 bit version
                SHELL(ENVIRON$("COMSPEC") + " /C " + mycommand, 0)
               
              END FUNCTION
              '------------------/PBMain
              Tested with both 32bit and 64bit versions of 7-zip installed on Wiin7 and Win10 systems
              Rgds, Dave

              Comment


              • #8
                What is $DQ ?
                is it a some kind of separator?

                Comment


                • #9
                  $DQ is a built-in equate in later versions of PowerBASIC. It is the Double Quote character; it is easier to read than using three quote characters like """

                  Same as using CHR$(34).
                  "Not my circus, not my monkeys."

                  Comment


                  • #10
                    Thanks

                    Comment


                    • #11
                      Hi Dave,

                      I hadn't looked at "SHELL(ENVIRON$("COMSPEC") + " /C DIR *.* > filename.txt")" because it runs asynchronously and I must have the result before my
                      program continues.

                      I have now just noticed that you can write the same thing as a statement and the program will pause.

                      Thank you very much for the sample code, I was vaguely aware of the "COMSPEC" sample above and understood it but didn't know that you could also
                      run a program in the same way.

                      Comment


                      • #12
                        Robert,
                        I hadn't looked at "SHELL(ENVIRON$("COMSPEC") + " /C DIR *.* > filename.txt")" because it runs asynchronously and I must have the result before my
                        program continues.
                        No, the above shell is the synchronous method which you want.

                        To run asynchronous would be:
                        pid??? = SHELL(ENVIRON$("COMSPEC")


                        Might also want to hide the window by passing a window style of 0 (see shell in help.)
                        SHELL MyApp$,0 (optional EXIT TO exitvar&)

                        Comment


                        • #13
                          I don't know the answer to this but, instead of using SHELL and having to worry about COMSPEC or spaces or possible variations in command-shells based on Windows version (e.g., Win 11 was just announced), perhaps you can use either the ShellExecuteEx() or CreateProcess() Windows functions to launch your program and redirect the output.

                          I know these work for 32-bit programs, I just don't know if a 32-bit program may successfully launch a 64-bit program this way.
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            I will go back to "Zlib" which works perfectly but I thought it would be worth using something more modern!

                            I want to say I already recently addressed this, but if you are willing to change compression programs anyway you may want to look at the compression functions provided in the Windows' API==>


                            https://docs.microsoft.com/en-us/win...ression-portal
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              Maybe I am too old now (86.5) but I can't get either to work!!

                              Code:
                              LOCAL mycommand AS STRING
                              mycommand = $DQ + "C:\Program Files\7-Zip\7z.exe" + $DQ + " l C:\D_Drive\1-test.zip > C:\D_Drive\1-test.txt"
                              SHELL ENVIRON$("COMSPEC") + " /C " + mycommand, 0
                              SHELL(ENVIRON$("COMSPEC") + " /C " + mycommand, 0)

                              Comment


                              • #16
                                Code:
                                FUNCTION PBMAIN AS LONG  'c:\zip\test.bas
                                 KILL "myzip.zip"
                                 SHELL ENVIRON$("COMSPEC") + " /C ""C:\Program Files\7-Zip\7z.exe"" a myzip.zip *.*",0
                                 SHELL ENVIRON$("COMSPEC") + " /C ""C:\Program Files\7-Zip\7z.exe"" l myzip.zip *.* > me.txt",0
                                 SHELL "notepad.exe me.txt"
                                END FUNCTION

                                Comment


                                • #17

                                  https://info.nrao.edu/computing/guid...and-line-guide
                                  I'm studying 7zip.
                                  The double quotes can get a bit tricky if an extra space is needed between the program name and the parameters/arguments.
                                  Added some helpers: a macro, a function and just shell statements.
                                  Code:
                                  MACRO mzip(sParms) = SHELL ENVIRON$("COMSPEC") + " /C ""C:\Program Files\7-Zip\7z.exe""" + " " + sParms,0
                                  
                                  FUNCTION PBMAIN AS LONG  'c:\zip\test.bas
                                   KILL "me.txt":KILL "myzip.zip":ERRCLEAR
                                   mzip("a myzip.zip *.*")
                                   mzip("l myzip.zip *.* > me.txt")
                                   IF ISFILE("me.txt") THEN SHELL "notepad.exe me.txt" ELSE ? "ZIP FAILED"
                                  
                                   KILL "me.txt":KILL "myzip.zip":ERRCLEAR
                                   zip "a myzip.zip *.*"
                                   zip "l myzip.zip *.* > me.txt"
                                   IF ISFILE("me.txt") THEN SHELL "notepad.exe me.txt" ELSE ? "ZIP FAILED"
                                  
                                   KILL "me.txt":KILL "myzip.zip":ERRCLEAR
                                   SHELL ENVIRON$("COMSPEC") + " /C ""C:\Program Files\7-Zip\7z.exe"" a myzip.zip *.*",0
                                   SHELL ENVIRON$("COMSPEC") + " /C ""C:\Program Files\7-Zip\7z.exe"" l myzip.zip *.* > me.txt",0
                                   IF ISFILE("me.txt") THEN SHELL "notepad.exe me.txt" ELSE ? "ZIP FAILED"
                                  END FUNCTION
                                  
                                  SUB zip(s AS STRING)
                                   SHELL ENVIRON$("COMSPEC") + " /C ""C:\Program Files\7-Zip\7z.exe""" + " "  + s,0
                                  END SUB

                                  Comment


                                  • #18
                                    When in doubt, try using ShellExecute.
                                    I had some problems in a program, trying to call up WordPad with SHELL("Wordpad.exe"), which failed.
                                    Changed to ShellExecute(0, "", "Wordpad.exe", "", "", %SW_SHOWNORMAL), which worked. Don't know why and didn't investigate it further either.

                                    Comment


                                    • #19
                                      ShellExecute runs asynchronously.
                                      He wants to redirect output to a file before continuing.

                                      To shell to wordpad.exe, SHELL "WRITE.EXE"

                                      Comment


                                      • #20
                                        Yes Mike, I just wanted to point out a difference - ShellExecute finds and starts "WordPad" directly, but SHELL does not - it calls up WordPad via a tiny Write.exe "launcher" program located in System32 folder.

                                        Comment

                                        Working...
                                        X