views:

234

answers:

3

I searched the site but 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#. seems like it should be pretty simple but I haven't seen anything online that quite matches what i'm looking for. Thanks in advance.

+5  A: 

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
+3  A: 

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
i'm close:Traceback (most recent call last): File "C:\Python Scripts\eodReports.py", line 14, in <module> 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
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
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
Traceback (most recent call last): File "C:\Python Scripts\eodReports.py", line 14, in <module> 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
Maybe you should post your code as another question. It's hard for me to debug it just by the traceback.
gradbot
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
A: 

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