How to run a command prompt EXE?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jld730
    New Member
    • Apr 2007
    • 34

    How to run a command prompt EXE?

    Hi All,

    How do you run an executable that normally has to be run from the Windows Command Prompt? The EXE of interest is normally done by typing mrsidsdw.exe <dir>/sidFile.sid, while in the specific directory of the EXE. I want to get this EXE to batch process (it normally runs one at a time). In the current case, I have the EXE and the files in the same directory.

    I have tried a few things, including the below code -- no luck. It runs without errors, but the EXE isn't executed b/c a file should be created out of it, but none is.

    Code:
    # Set the workspace.
    gp.Workspace = r"C:\Documents and Settings\jenn5214\My Documents\Projects\Process SDW"
    
    # List all the raster datasets
    sidList = gp.ListRasters("*", "SID")
    
    # Grab the first raster dataset.
    sid = sidList.Next()
    
    # Loop through each raster dataset
    while sid:
        os.system("mrsidsdw.exe " + sid)
        print sid
        sid = sidList.Next()
    
    print "done!"
    Any suggestions would be greatly appreciated!!!
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    does the
    Code:
    print sid
    line properly report s single string which corresponts to the file name you are expecting? Is mrsidsdw.exe in the path? When you say no errors, are you running the python program from the command line? And is it outputting anything at all?

    I'm a little fuzzy on a couple things because I don't recognize the gp module, and the .Next() method. I would be more likely to just take a list and use a
    Code:
    for item in list:
    method

    Originally posted by jld730
    Hi All,

    How do you run an executable that normally has to be run from the Windows Command Prompt? The EXE of interest is normally done by typing mrsidsdw.exe <dir>/sidFile.sid, while in the specific directory of the EXE. I want to get this EXE to batch process (it normally runs one at a time). In the current case, I have the EXE and the files in the same directory.

    I have tried a few things, including the below code -- no luck. It runs without errors, but the EXE isn't executed b/c a file should be created out of it, but none is.

    Code:
    # Set the workspace.
    gp.Workspace = r"C:\Documents and Settings\jenn5214\My Documents\Projects\Process SDW"
    
    # List all the raster datasets
    sidList = gp.ListRasters("*", "SID")
    
    # Grab the first raster dataset.
    sid = sidList.Next()
    
    # Loop through each raster dataset
    while sid:
        os.system("mrsidsdw.exe " + sid)
        print sid
        sid = sidList.Next()
    
    print "done!"
    Any suggestions would be greatly appreciated!!!

    Comment

    • jld730
      New Member
      • Apr 2007
      • 34

      #3
      Originally posted by dshimer
      does the
      Code:
      print sid
      line properly report s single string which corresponts to the file name you are expecting? Is mrsidsdw.exe in the path? When you say no errors, are you running the python program from the command line? And is it outputting anything at all?

      I'm a little fuzzy on a couple things because I don't recognize the gp module, and the .Next() method. I would be more likely to just take a list and use a
      Code:
      for item in list:
      method

      Oh, sorry... the gp module is specifc to my industry of GIS (gp = arcgisscripting .create() -- allows for geo-processing). The while loop is basically looping through all the images in a directory. The print statement simply prints out the current image file name, not important. No, mrsidsdw.exe is not in the print statement, if that is what you mean. No, I am running the python program from within Python. I was thinking that maybe i should be running the script from the command prompt and the script executes the EXE -- is that what I should be doing? ?? Sorry for my cluelessness -- I'm not a programmer by trade, obviously!

      Pardon my cluelessness again, but wouldn't the while loop and for item is list do the same thing?

      Thansk for your help!!!

      Comment

      • dshimer
        Recognized Expert New Member
        • Dec 2006
        • 136

        #4
        Let me back up for a second, the question about the print statement didn't have anything to do with the mrsidsdw. Since it is there the print statement should output something each time through the loop, I was just trying to find out if the problem was that the file names weren't being passed properly to the command prompt. For example if they were, the filenames should be printed out one at a time to show the status of the processing. However if you are running the command from within a python editor like 'pythonwin' or something like that then the output of the command may not stay on the screen long enough for you to see.

        I put a copy of mrsidsdw in a tmp directory. Then I went to a directory that contained a sid file, then I ran the following script called test.py by using the following command line (note that I have python fully installed and in my windows path).

        python test.py

        which contains
        Code:
        import os
        os.system(r'\tmp\mrsidsdw licking.sid')
        mrsidsdw generates the following screen. So you can see that it should always output something, even if it was an error.

        Z:\Laptop\PLSO_ Technical\Cherr yValley>python tmp.py
        mrsidsdw: (c) 2003 LizardTech, Inc. All rights reserved.

        Image data:
        width: 175000
        height: 140000
        has geo info: true
        X UL: 1885000.500000
        Y UL: 834999.500000
        X res: 1.000000
        Y res: -1.000000

        World file licking.sdw created.

        So you can see that the underlying premise is ok, there is just something in the application of it that would probably come out if you ran the python script from a command line and evaluated the output.

        Comment

        • jld730
          New Member
          • Apr 2007
          • 34

          #5
          Originally posted by dshimer
          Let me back up for a second, the question about the print statement didn't have anything to do with the mrsidsdw. Since it is there the print statement should output something each time through the loop, I was just trying to find out if the problem was that the file names weren't being passed properly to the command prompt. For example if they were, the filenames should be printed out one at a time to show the status of the processing. However if you are running the command from within a python editor like 'pythonwin' or something like that then the output of the command may not stay on the screen long enough for you to see.

          I put a copy of mrsidsdw in a tmp directory. Then I went to a directory that contained a sid file, then I ran the following script called test.py by using the following command line (note that I have python fully installed and in my windows path).

          python test.py

          which contains
          Code:
          import os
          os.system(r'\tmp\mrsidsdw licking.sid')
          mrsidsdw generates the following screen. So you can see that it should always output something, even if it was an error.

          Z:\Laptop\PLSO_ Technical\Cherr yValley>python tmp.py
          mrsidsdw: (c) 2003 LizardTech, Inc. All rights reserved.

          Image data:
          width: 175000
          height: 140000
          has geo info: true
          X UL: 1885000.500000
          Y UL: 834999.500000
          X res: 1.000000
          Y res: -1.000000

          World file licking.sdw created.

          So you can see that the underlying premise is ok, there is just something in the application of it that would probably come out if you ran the python script from a command line and evaluated the output.

          I have seen that output when running the EXE from the command prompt. When I add Python to the mix, no luck. The looping works (it prints each raster), and I see the command prompt flash by briefly, but no .sdw file is created.

          If I try running your [edited for me] script from the command prompt I get an error of "'python' is not recognized...'. That error along with your comment (note that I have python fully installed and in my windows path) above clued me in. Eventually I executed the command as C:\Python24\pyt hon process_sid.py, and that worked!!! .sdw files were created for all sids in the directory.

          It occurs to me now that I don't even need the GP to get a list of rasters (GP can be a processing hog). How do you get a list of only .sid files in a particular directory -- I know that seems very elementary, I can do it with GP, but...

          Comment

          • dshimer
            Recognized Expert New Member
            • Dec 2006
            • 136

            #6
            I only have a second and may contact you off list as one geomatic professional to another because I use python for tons of stuff. But quickly , check this out.
            Code:
            import glob
            filelist=glob.glob('*.tif')
            print filelist
            for filename in filelist:
            	print filename
            which returns

            Z:\Laptop\PLSO_ Technical\Cherr yValley>python test.py
            ['CherryValley.t if', 'CherryValleyDl g.tif', 'CherryValleyDo qq.tif', 'CherryValleyDr g.tif', 'OverlayOsip.ti f', 'CherryValley1f t.tif']
            CherryValley.ti f
            CherryValleyDlg .tif
            CherryValleyDoq q.tif
            CherryValleyDrg .tif
            OverlayOsip.tif
            CherryValley1ft .tif

            Notice the list printed by
            Code:
             print filelist
            and the individual file names printed out (which could easily be processed in any way) within the for loop.

            Comment

            • jld730
              New Member
              • Apr 2007
              • 34

              #7
              Originally posted by dshimer
              I only have a second and may contact you off list as one geomatic professional to another because I use python for tons of stuff. But quickly , check this out.
              Code:
              import glob
              filelist=glob.glob('*.tif')
              print filelist
              for filename in filelist:
              	print filename
              which returns

              Z:\Laptop\PLSO_ Technical\Cherr yValley>python test.py
              ['CherryValley.t if', 'CherryValleyDl g.tif', 'CherryValleyDo qq.tif', 'CherryValleyDr g.tif', 'OverlayOsip.ti f', 'CherryValley1f t.tif']
              CherryValley.ti f
              CherryValleyDlg .tif
              CherryValleyDoq q.tif
              CherryValleyDrg .tif
              OverlayOsip.tif
              CherryValley1ft .tif

              Notice the list printed by
              Code:
               print filelist
              and the individual file names printed out (which could easily be processed in any way) within the for loop.

              Awesome, that works great, faster then GP! Thanks for your help, and feel free to contact me off list. You'd be a great resource---maybe I can be of help to you, somehow ;-)

              Final code, run from command prompt in directory where SIDs are located:
              Code:
              import glob, os
              filelist=glob.glob('*.sid')
              print filelist
              for filename in filelist:
                  print filename
                  os.system(r"C:\Temp\mrsidsdw.exe " + filename)

              Comment

              • jld730
                New Member
                • Apr 2007
                • 34

                #8
                Originally posted by jld730
                Awesome, that works great, faster then GP! Thanks for your help, and feel free to contact me off list. You'd be a great resource---maybe I can be of help to you, somehow ;-)

                Final code, run from command prompt in directory where SIDs are located:
                Code:
                import glob, os
                filelist=glob.glob('*.sid')
                print filelist
                for filename in filelist:
                    print filename
                    os.system(r"C:\Temp\mrsidsdw.exe " + filename)

                One last question... when I run the script in the command prompt, I have to type C:\Python24\pyt hon script.py, but you just had to type python script.py -- is there something that allows this????

                Comment

                • dshimer
                  Recognized Expert New Member
                  • Dec 2006
                  • 136

                  #9
                  Originally posted by jld730
                  One last question... when I run the script in the command prompt, I have to type C:\Python24\pyt hon script.py, but you just had to type python script.py -- is there something that allows this????
                  Let me answer at the introductory level since it will be here forever and you never know what level a reader may be at.
                  Assuming python is installed in c:\python24

                  1. Right click "my computer"
                  2. Select "Properties "
                  3. Select "Advanced"
                  4. Select "Environmen t Variables"
                  5. Under "System Variables" select "Path"
                  6. Select "Edit"
                  7. In the "Variable Value" box, position the cursor all the way at the end and paste ;c:\python2
                  8. Hit all the OK's necessary to get all the way out.
                  The string "c:\python2 4" adds that directory to the executable search path and the semi colon (;) is the separator between paths. This will also work with the path where your mrsidsdw.exe would be found. I have a utility directory that I keep for various command line programs that I rarely use and put that directory in the path. If I have a program that installs several command line programs in one place I can add that one using the same method. Then any time you start a command prompt window those directories will be searched for the name of an executable, or batch file matching the word you type eg: python.

                  Comment

                  Working...