How to run an EXE, with argument, capture output value

Tim Harig usernet at ilthio.net
Fri Nov 19 19:27:23 CET 2010


C:\Documents and Settings\Tim Harig\My Documents\autoCalc>dir
 Volume in drive C has no label.
 Volume Serial Number is 30D9-35E0

 Directory of C:\Documents and Settings\Tim Harig\My Documents\autoCalc

11/19/2010  12:20 PM    <DIR>          .
11/19/2010  12:20 PM    <DIR>          ..
11/19/2010  12:19 PM               686 autoCalc.pys
               1 File(s)            686 bytes
               2 Dir(s)  16,343,552,000 bytes free

C:\Documents and Settings\Tim Harig\My Documents\autoCalc>type autoCalc.pys
# autoCalc.pys: The "pys" extension indicates that it should be run under
# Windows Script Host

# perform the calculation using Windows calculator
keySequence = ['2', '{+}', '2', '=', '^c', '%{F4}']
WshShell = WScript.CreateObject("WScript.Shell")
calculator = WshShell.Run("calc")
WshShell.AppActivate("calc")
WScript.Sleep(1000)
for currentKey in keySequence:
        WshShell.SendKeys(currentKey)
        WScript.Sleep(100)

# write the results to notepad and same as demo.txt
keySequence = ['result: ', '^v', '^s',
        'c:\\Documents and Settings\\Tim Harig\\My Documents\\autoCalc\\demo.txt',
        '~', '%{F4}']
notepad = WshShell.Run("notepad")
WshShell.AppActivate("notepad")
WScript.Sleep(1000)
for currentKey in keySequence:
        WshShell.SendKeys(currentKey)
        WScript.Sleep(100)

C:\Documents and Settings\Tim Harig\My Documents\autoCalc>cscript.exe autoCalc.pys
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Debugging extensions (axdebug) module does not exist - debugging is disabled..

C:\Documents and Settings\Tim Harig\My Documents\autoCalc>type demo.txt
result: 4
C:\Documents and Settings\Tim Harig\My Documents\autoCalc>



More information about the Python-list mailing list