How can I see the results and how can the windows Close - vb.net

I have this code, and I´m calling one Java program to pass the aurguments, that I select in VB.
My issues is:
The cmd windows open, but I can't see what the program is doing, I only see on the window the arguments that are pass, and when finish, the window don't close.
enter image description here
and when program stop, the windows don't close.
enter image description here
This is the result if I run by command line
enter image description here
txbSendCommand.Text = "java -jar FACTEMICLI-2.5.16-33194-cmdClient.jar " & "-i " & TextBox1.Text & "" & " -n " & stNIF & " -p " & stPassword & " -a " & iAno & " -m " & iMes & " -op " & stvalidar & stFicheiroEscolhido & " -o c:\saft\outputfile.xml"
Dim stcaminhoexterno As String
If stErrorMessage = False Then
Dim app As New ProcessStartInfo("cmd.exe") With {.RedirectStandardInput = True, .UseShellExecute = False, .CreateNoWindow = False}
Dim myProcess As New Process
myProcess = Process.Start(app)
Dim arguments As String = txbSendCommand.Text
myProcess.StandardInput.WriteLine(arguments)
myProcess.Close()
End If
How can I see the results in the external windows, or better even in a windows or something that I can have in the form.
How Can the window close after the process is done

Related

How to use multiple commands with path references in command prompt

I'm new to this forum, so please correct me if I'm asking this the wrong way or not specific enough..
While coding in VB.NET I'm trying to pass multiple commands, and an argument containing a reference to a path:
Dim p As New Process
Dim pi As New ProcessStartInfo
pi.Arguments = " " & "/K """ & "C:\program files\gdal\gdalshell.bat" & """ & " & "cd C:\program files\gdal" & _
" & " + "gdal_translate" + " -of Jpeg -outsize 2000 2000 """ & "D:\box sync\my box (907070)\RIS_RHDHV_Overgang\GDAL\test2.xml" & """ "
pi.FileName = "C:\windows\syswow64\cmd.exe"
p.StartInfo = pi
p.Start()
The command prompt returns:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
I did some research on the matter and found:
vb.net How to pass a string with spaces to the command line
This however still doesn't seem to solve the problem. When I execute the following code, it runs without issues:
Dim p As New Process
Dim pi As New ProcessStartInfo
pi.Arguments = " " & "/K """ & "C:\program files\gdal\gdalshell.bat" & """ & " & "cd C:\program files\gdal" & _
" & " + "gdal_translate"
pi.FileName = "C:\windows\syswow64\cmd.exe"
p.StartInfo = pi
p.Start()
To me it looks like the problem is caused by the path reference inside an argument. I have read and used the different answers for using multiple commands, without any luck.
It would be great if someone could help me on this topic.
Kind regards,
Stuart
First of all, as a little note you don't need to add a space in the beginning of the arguments. That's only done when you write the entire command (including the executable) in one line.
Now, the correct way to pass a path as an argument is to surround it with quotes. So something like this should do:
(please note that I have also shortened some unnecessary concatenations, etc.)
pi.Arguments = "/K """"C:\program files\gdal\gdalshell.bat"" & cd ""C:\program files\gdal"" & " & _
"gdal_translate -of Jpeg -outsize 2000 2000 ""D:\box sync\my box (907070)\RIS_RHDHV_Overgang\GDAL\test2.xml"""""
EDIT:
I found that you also have to put the entire text after /K in quotes for it to work.

Argument issue when using vb.net shell ()

I was trying to use shell function in vb.net to run a program and then write/export the result else where , it works on win8 but not XP !! The command line prints this error
'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
Dim save as String="C:\exported.txt"
Dim command As String = tempPath & "app.exe -f " & IO.Path.GetTempPath & " -o " & save & " & pause"
shell("cmd /c " & command, AppWinStyle.NormalFocus, True)
Process.Start(IO.Path.Combine(tempPath, "app.exe"), "-f """ & IO.Path.GetTempPath & """ -o """ & Save() & """ & pause")
You hace a folder in the command with a space in it (probably the documents and Settings folder). cmd.exe has no way of telling whether the space is in the file name or the end of the file name, unless you put the file name in "" - like "C:\doxuments and Settings..."
Dim command As String = tempPath & "app.exe -f \"" & IO.Path.GetTempPath & "\" -o \"" & save & "\" & pause"
(\" is intended to escape a " so that it translates to embedding a " in the string. I'm not sure what the VB syntax is.)
EDIT:
I think #Roy van der Velde below has the 'escaping correct. The final part not checked is tempPath. See my comment on the question.

Pass Arguments to EXE

I have a button with this code
Process.Start("start system\now.exe")
Now I'd like to pass arguments from the Texbox but I dont know how . I've tried :
Process.Start("system\now.exe -connect -ip 127.0.0.1 -PORT 910 " & "-USER " & TextBox1.Text & "-PWD " & TextBox1.Text " -serial x4r7680")
But does not seem to work. Any idea guys?
You need to pass in a ProcessStartInfo object with the arguments. See the page in the MSDN docs for info on the ProcessStartInfo class.
Based on a cursory reading of the document, your example would look like this:
Dim startInfo as ProcessStartInfo("system\now.exe")
startInfo.Arguments = "-connect -ip 127.0.0.1 -PORT 910 " & "-USER " & TextBox1.Text & "-PWD " & TextBox1.Text " -serial x4r7680"
Process.Start(startInfo)
Look at the ProcessStartInfo class. An example:
Dim startInfo As New ProcessStartInfo("IExplore.exe")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(startInfo)
startInfo.Arguments = "www.northwindtraders.com"
Process.Start(startInfo)
but you may also be able to use another overload of the Start method to pass your command line arguments. Again an example:
' Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
or in your case:
Process.Start("system\now.exe","-connect -ip 127.0.0.1 -PORT 910 " & "-USER " & TextBox1.Text & "-PWD " & TextBox1.Text " -serial x4r7680")
or
Dim startInfo As New ProcessStartInfo("system\now.exe")
Process.Start(startInfo)
startInfo.Arguments = "-connect -ip 127.0.0.1 -PORT 910 " & "-USER " & TextBox1.Text & "-PWD " & TextBox1.Text " -serial x4r7680"
Process.Start(startInfo)
you need to be careful if TextBox1.Text contains a space as (depending on how your program parses the command line options) you might end up parsing them incorrectly unless you surround them in quotes.
Ok, Im sure the problem you have are the quotes "" of the parameters.
This example is using argument parameters.
Try with this:
Process.Start("system\now.exe", """" & "-connect -ip 127.0.0.1 -PORT 910 " & "-USER " & lblSubirLlamadasTimeInterval.Text & "-PWD " & lblSubirLlamadasTimeInterval.Text & "-serial x4r7680""")

run 2 seperate cmd commands from vb forms appliaction

i want my program to open cmd , change directory and then do the command : "copy /B file1 file2 output"
this is what i have at the moment. but all that happens is a cmd window flashes for a second but no file gets created
Dim cmd1 As String
Dim cmd2 As String
cmd1 = "cd " & FolderFromFileName(imagename)
cmd2 = "copy /B " & NameOnlyFromFullPath(imagename) & "+" & "TEMP.txt" & " " & TextBox1.Text
Shell("cmd /c" & " " & cmd1 & " " & cmd2, AppWinStyle.NormalFocus)
please help, thanks :)
Do you really need to have a command prompt appear? You could do all this without a separate process by using the system.io library. If you really need the cmd prompt you can create a process.
Dim NewProcess = New Process
' a new process is created
NewProcess.StartInfo.UseShellExecute = False
' redirect IO
' PVWCmd.StartInfo.RedirectStandardOutput = True
' PVWCmd.StartInfo.RedirectStandardError = True
' PVWCmd.StartInfo.RedirectStandardInput = True
' don't even bring up the console window
NewProcess.StartInfo.CreateNoWindow = False
' executable command line info
NewProcess.StartInfo.FileName = "cmd"
NewProcess.StartInfo.WorkingDirectory = "C:\"
' NewProcess.StartInfo.Arguments = +" > """ + "LogFile.log" + """ 2>&1"
NewProcess.Start()

VB piping STDOUT from cmd.exe output to to write to textbox

I'm trying to adapt a VBscript that runs the QWINSTA command against a text file I've defined as an array and displays the results in a text file.
after looking at numerous examples on Stack and other sites this is my latest iteration, that displays everything except the STDOUT, which is really what I'm after, not sure if it's an issue with how I'm calling the command piping the output or what. I'm also passing two variables as arguments to complicate matters :)
The Text file that stdout is piped to is empty after the FOR Loop completes
I may be going the long way about this, most of my past experience with this is in BATCH
For Each server In RemotePC
'The Query is launched
Dim Query As New ProcessStartInfo
Query.WorkingDirectory = "c:\"
Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
Query.FileName = "QWINSTA"
Query.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(Query)
'Call Shell("QWINSTA /SERVER:" & server & " " & profile & " >C:\Windows\Temp\SFOUT.txt", vbHidden, Timeout:=5000)
Dim SFOUT As New IO.StreamReader("C:\windows\temp\SFOUT.txt")
'Results are Echoed to TextboxResults
TextBoxResults.Text &= "__________________________________________________________" & ControlChars.CrLf
TextBoxResults.Text &= server & ControlChars.CrLf
TextBoxResults.Text &= SFOUT.ReadToEnd & ControlChars.CrLf
SFOUT.Close()
Next
Here is what the working code in VBscript looks like
For Each server In RemotePC
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set StrDir = "QWINSTA /SERVER:" & server & " " & profile
'The Query is launched
Set oExec = WshShell.Exec(StrDir)
'We wait for the end of process
Do While oExec.Status = 0
WScript.Sleep 500
Loop
'We scan and only display the command output of commands without NULL output
Do While oExec.StdOut.AtEndOfStream <> True
Results.WriteLine("__________________________________________________________")
Results.WriteLine(server)
Results.WriteLine oExec.StdOut.ReadLine
Loop
NEXT
Any help is appreciated
#Nilpo
this is what I get back
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console Matt 1 Active
I've built something similar to this using QWINSTA and piping the output in batch and it works flawlessly, just having issues adapting this.
here's the last thing I tried I tried to simplify things by calling something as basic as notepad.exe, and even trying to define the environment variable thinking it's not reading %PATH%, in reply to #Nilpo
Dim Query As New Process
Query.StartInfo.FileName = "notepad.exe"
'Query.StartInfo.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
Query.StartInfo.UseShellExecute = False
Query.StartInfo.RedirectStandardOutput = True
Query.StartInfo.WindowStyle = ProcessWindowStyle.Normal
Environment.SetEnvironmentVariable("PATH", "%PATH%;" & "C:\Windows\System32\notepad.exe")
Query.Start()
You should have a space after the output redirection.
Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
Should be
Query.Arguments = server & " " & profile & " > C:\Windows\Temp\SFOUT.txt"
That being said, the only thing I can see that may not work is here.
Query.WorkingDirectory = "c:\"
Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
Query.FileName = "QWINSTA"
Query.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(Query)
I'm not sure that you can use arguments in this fashion. Your output redirection is being read as a literal argument when it's really not. In a command line environment it's interpreted as a separate command, not an argument to the first command. It's very similar to piping in that manner. Supplying this as an argument in your code probably won't work as expected. You may want to go back to the Call Shell method. That will execute the command as if it were on the command line.
[Edit]
I was correct. Here is the proper way to do this using your method.
Query.WorkingDirectory = "c:\"
Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
Query.FileName = "QWINSTA"
Query.WindowStyle = ProcessWindowStyle.Hidden
'Redirect output
Query.UseShellExecute = False
Query.RedirectStandardOutput = True
Process.Start(Query)
'Read the output
Dim output As String = Process.StandardOutput.ReadToEnd
' wait for the process to terminate
Process.WaitForExit