Process.Start() argument question

lee_umney

Member
Joined
Jun 29, 2011
Messages
6
Programming Experience
Beginner
I am trying to start CMD and pass a command to it and then monitor it in the app I am writing. The user enters the information that makes the commandline and when they click Go it does start CMD but straight away it says it can't find the file.
Is there any way I can view what is being sent to the CMD prompt to double check that this is correct?
Do I need to specify the path to the file I want it to run, i.e. cscript c:\test\Migrate.wsf?
Any help would be appreciated.
Lee
 
Is there any way I can view what is being sent to the CMD prompt to double check that this is correct?
You can redirect the output and read it in your app, see ProcessStartInfo.RedirectStandardOutput Property (System.Diagnostics)
Do I need to specify the path to the file I want it to run, i.e. cscript c:\test\Migrate.wsf?
Have a look at this: ProcessStartInfo.WorkingDirectory Property (System.Diagnostics)

By the way, cscript is an executable (.exe) that you can use as filename for the Process, no need to pass it as argument to cmd.exe.
In the case of cscript.exe path it is a Windows system tool that is available to call from any path.
Its file argument must be available from the working directory or else specified by path.
 
Didn't work

I changed the program to cscript and it still didn't work, my code is below, if some-one could take a look over it I would be grateful.

strCMD = strcommandline
Dim myProcess As New Process()
Dim myProcessStartInfo As New ProcessStartInfo()
myProcessStartInfo.FileName = "cscript"
myProcessStartInfo.Arguments = strCMD
myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardOutput = True
myProcessStartInfo.RedirectStandardInput = True
myProcess.StartInfo = myProcessStartInfo
Try
myProcess.Start()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
myProcess.Start()
Dim myStreamReader As StreamReader = myProcess.StandardOutput
' Read the standard output of the spawned process.
Dim myString As String = myStreamReader.ReadLine()
TextBox1.Text = TextBox1.Text & vbCrLf & myString
myProcess.WaitForExit()

Thanks in advance,
Lee
 
The path you posted didn't have any spaces in it, but if that could happen do quote it in order for it to be read as a single argument.
myProcessStartInfo.Arguments = """" & strCMD """"

where strCMD in your context must be the full path to the file.

Btw, have a look at With statement, it can simplify your coding and make it less wordy thus more readable.
 
Slight progress

Thanks for the help so far, the app now seems to throw up 2 cmd type boxes and I think the process is being run on the second, there any way of capturing the second cmd box?
The argument I am trying to pass is this:
c:\test\w2kmiguser.wsf //job:backup /Store:v /ConfigFile:W2KMigUser.xml
This is made up from the UI from the user adding/selecting option and the source directory is found from this:
sPath = Trim(System.Reflection.
Assembly.GetExecutingAssembly().Location)
nIndex = sPath.LastIndexOf("/")
sPath = sPath.Substring(0, nIndex)

but even if I specify the location as c:\test\w2kmiguser.wsf I can't tell if it is working or not due to the 2 cmd boxes.
Thanks in advance
Lee
 
The argument I am trying to pass is this:
c:\test\w2kmiguser.wsf //job:backup /Store:v /ConfigFile:W2KMigUser.xml
Each string from the commandline separated by a space is interpreted as one argument, to pass one argument containing spaces it must be quoted.

What you have there is not one argument, it is four arguments, one which is the path to the script file, one which is argument to csript to select which script job to run, and two of which is passed as arguments to the script itself.

So you have to be careful to pass the correct information to the correct executable. If you for example passed that string as a single argument to cscript (by quoting the whole string), cscript would not be able to find such a file. Also, you are passing a xml path argument to the wsf, I don't know how that is supposed to be used by the script, but make sure that path unqualified is valid in the context of the operation, if not qualify it or set a proper working directory.
the app now seems to throw up 2 cmd type boxes and I think the process is being run on the second
The only way for that to happen is if a script starts a non-commandline process (exits current command process), which in turn starts its own commandline process. It would not be possible to read output from secondary processes.
 
Back
Top