6

I searched the site, but I didn't see anything quite matching what I was looking for. I created a stand-alone application that uses a web service I created. To run the client I use:

C:/scriptsdirecotry> "run-client.bat" param1 param2 param3 param4

How would I go about coding this in Python or F#. It seems like it should be pretty simple, but I haven't seen anything online that quite matches what I'm looking for.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Ramy
  • 20,541
  • 41
  • 103
  • 153
  • I really appreciate both answers. Right now, i'm closer to getting python to work. I don't see why i should pick one answer over another though and i don't have enough "reputation" to vote answers up. – Ramy May 27 '10 at 21:33

3 Answers3

14

Python is similar.

import os
os.system("run-client.bat param1 param2")

If you need asynchronous behavior or redirected standard streams.

from subprocess import *
p = Popen(['run-client.bat', param1, param2], stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
p.wait() # wait for process to terminate
gradbot
  • 13,732
  • 5
  • 36
  • 69
  • i'm close: Traceback (most recent call last): File "C:\Python Scripts\eodReports.py", line 14, in p = subprocess.Popen([batch, clientID, username, password, output], stdout=subprocess.PIPE, stderr=subprocess.PIPE) File "C:\Python25\lib\subprocess.py", line 586, in __init__ errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "C:\Python25\lib\subprocess.py", line 681, in _get_handles p2cread = self._make_inheritable(p2cread) File "C:\Python25\lib\subprocess.py", line 722, in _make_inheritable DUPLICATE_SAME_ACCESS) TypeError: an integer is required – Ramy May 27 '10 at 21:41
  • Try setting stdin also even if you don't need it. `p = subprocess.Popen([batch, clientID, username, password, output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)` I believe this is fixed in python 2.6. – gradbot May 28 '10 at 01:06
  • sorry there is such a delay between reponses. This is a side project so I don't have much time to work on it. See the exception below, please: – Ramy May 28 '10 at 20:02
  • Traceback (most recent call last): File "C:\Python Scripts\eodReports.py", line 14, in p = subprocess.Popen([batch, clientID, username, password, output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) File "C:\Python25\lib\subprocess.py", line 593, in __init__ errread, errwrite) File "C:\Python25\lib\subprocess.py", line 750, in _execute_child args = list2cmdline(args) File "C:\Python25\lib\subprocess.py", line 502, in list2cmdline needquote = (" " in arg) or ("\t" in arg) TypeError: argument of type 'int' is not iterable – Ramy May 28 '10 at 20:08
  • Maybe you should post your code as another question. It's hard for me to debug it just by the traceback. – gradbot May 28 '10 at 23:11
  • actually, i switched to the os.system version. My original intent was to run the same batch script with different parameters. the second method (popen) looked like it would lend itself better to that. Instead I ended up creating another batch file that would run the series of calls to the original batch file and THEN used the os.system call on the one master batch script. thanks for the help!! – Ramy Jun 01 '10 at 19:02
8

In F#, you could use the Process class from the System.Diagnostics namespace. The simplest way to run the command should be this:

open System.Diagnostics
Process.Start("run-client.bat", "param1 param2")

However, if you need to provide more parameters, you may need to create ProcessStartInfo object first (it allows you to specify more options).

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
2

Or you can use fsi.exe to call a F# script (.fsx). Given the following code in file "Script.fsx"

#light

printfn "You used following arguments: "
for arg in fsi.CommandLineArgs do
  printfn "\t%s" arg

printfn "Done!"

You can call it from the command line using the syntax:

fsi --exec .\Script.fsx hello world

The FSharp interactive will then return

You used following arguments:
        .\Script.fsx
        hello
        world
Done!

There is more information about fsi.exe command line options at msdn: http://msdn.microsoft.com/en-us/library/dd233172.aspx

Huusom
  • 5,654
  • 1
  • 17
  • 15