How to use command lines in vb.net - vb.net

I m developing a windows application that will use pdf2text pilot software which supports command line. In this application user needs to specify location of the pdf file. I m able to open cmd but could not pass commands to it or somehow my commands are not getting executed.
Imports System
Imports System.IO
Imports System.Diagnostics.Process
Imports System.Diagnostics.ProcessStartInfo
Public Class EDCS
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim dlgrslt As DialogResult = OpenFileDialog1.ShowDialog()
Dim fnames As String() = OpenFileDialog1.FileNames
Dim txtfnames As String
For i = 0 To fnames.Length - 1
If TextBox1.Text = "" Then
TextBox1.Text = fnames(i)
Else
TextBox1.Text = TextBox1.Text + " / " + fnames(i)
End If
txtfnames = fnames(i).Replace(".pdf", ".txt")
File.Create(txtfnames).Dispose()
Dim convertcommand As String = "textextract """ & fnames(i) & """ /to """ & txtfnames & """"
'/c will exit cmd and /k will keep it open
'Shell("cmd.exe /c textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt"")
'SendKeys.Send(convertcommand)
'SendKeys.Send("{ENTER}")
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
'p.StartInfo.WorkingDirectory = "C:\Program Files (x86)\Two Pilots\PDF2Text Pilot"
'p.StartInfo.Arguments = "textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt""
p.StartInfo.UseShellExecute() = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.Start()
p.StandardInput.WriteLine(convertcommand)
'Dim process As New Process()
'process.StartInfo.FileName = "cmd.exe "
'process.StartInfo.Verb = "runas"
'process.StartInfo.UseShellExecute = False
'process.StartInfo.RedirectStandardInput = True
'process.StartInfo.RedirectStandardOutput = True
'process.Start()
'process.StandardInput.WriteLine("textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt"")
'process.StandardInput.WriteLine("exit")
'process.Close()
Next
End Sub
End Class
OS: Windows 7
vb.net developer
Thanks in Advance

OK, here you go, this should work, turns out that pdf2text has a bug in it, but I managed to get it working by passing a couple of blank lines before the command runs and then also adding a wait for the utility to complete before the command prompt closes. You may need to increase that wait for large files I guess. I get some "The handle is invalid" messages in the command prompt and I suspect they are from pdf2text too, but it looks like its safe to ignore them.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dlgrslt As DialogResult = OpenFileDialog1.ShowDialog()
Dim fnames As String() = OpenFileDialog1.FileNames
Dim txtfnames As String
For i = 0 To fnames.Length - 1
If TextBox1.Text = "" Then
TextBox1.Text = fnames(i)
Else
TextBox1.Text = TextBox1.Text + " / " + fnames(i)
End If
txtfnames = fnames(i).Replace(".pdf", ".txt")
File.Create(txtfnames).Dispose()
Dim convertcommand As String = "textextract.exe """ & fnames(i) & """ /to """ & txtfnames & """"
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.Arguments = " /k"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = False
p.Start()
Using sw As StreamWriter = p.StandardInput
If sw.BaseStream.CanWrite Then
sw.WriteLine()
sw.WriteLine()
sw.WriteLine(convertcommand)
p.WaitForExit(3000)
End If
End Using
Next
End Sub

Related

How to get the unique Process ID from a 3rd party exe

Ive created a screen recorder and stream recorder that can run at the same time. I'm using FFMPEG to perform the recordings which are triggered using the Process.Start() function. Now if both recording options are running, I have 2 FFMPEG programs running and 2 Console windows running. So, I need to be able to close or exit either recording options independently without it effecting the other.
So far this is my code...
Dim cmdstr = "/k ffmpeg.exe -y -probesize 10M -rtbufsize 1500M -f dshow -i audio=""" &
Audio_name & """ -acodec pcm_s16le -f gdigrab -framerate 60 -i desktop -vcodec libx264 -qp 0
-threads 0 -crf 18 -preset ultrafast -tune zerolatency " & str & "\Recordings\ScreenRecorder"
& FileTime & ".mkv"
If TestFF = True Then
MsgBox("1. Test Mode: " & cmdstr)
Process.Start("cmd.exe", "/k ffmpeg.exe -list_devices true -f dshow -i dummy") ' This lists all devices
Process.Start("cmd.exe", cmdstr)
RTBStreamData1.Text = cmdstr
RecordingInProcess = True
RecordingOn = True
Else
Dim startInfo As New ProcessStartInfo("cmd.exe")
startInfo.Arguments = cmdstr
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
Process.Start(startInfo)
RecordingInProcess = True
RecordingOn = True
Dim ProcID As Integer = Process.GetCurrentProcess.Id
Label13.Text = ProcID.ToString
I am able to get the ID of the application itself, but it is the individual processes of ffmpeg and the associated console window that I need to be able to close at the same time.
any form of help would be really appreciated. Thank you.
In the image above you can see that i was able to track the first process, but not the second!!!
If I well understood: A trick you can use might be you pass a “FAKE” custom argument to each process you’re going to start by Process.Start(). After that looping running processes you can recognize your target process.
The code below gives an idea. (There is to change something in the SQL String in order to avoid SQL injection)
Dim PROCESS_CUSTOM_ARG As String = "UNIQUE_STRING_ID_HERE" '& _1 also add an integer incremental for each process
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim startInfo As New ProcessStartInfo("cmd.exe")
startInfo.Arguments = " - a lot of your Arguments here"
startInfo.Arguments &= " - " & PROCESS_CUSTOM_ARG
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
Process.Start(startInfo)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim searcher As New System.Management.ManagementObjectSearcher("root\CIMV2", "SELECT ProcessId, CommandLine FROM Win32_Process WHERE CommandLine LIKE '%" & PROCESS_CUSTOM_ARG & "' ")
For Each p As System.Management.ManagementObject In searcher.Get()
Dim ProcessId As String = p("ProcessId")
Dim CommandLine As String = p("CommandLine")
Console.WriteLine($"CommandLine: {CommandLine} ProcessId: {ProcessId}")
Dim Proc As Process = Process.GetProcessById(ProcessId)
Proc.Kill()
Next
End Sub

Search For ID number In TextFile vb.net

This is what i need to do, I click on the openfile button and brings up the openfile dialog box. I open a textfile and it gets displayed in the informationbox.text field, at the same time i would like to search that file for an ID number and display it in the IDbox.text field.
i have searched other forums, but they just use the Replace method or other methods that i don't know about. It becomes too confusing.
This is what i have so far -
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
Dim oReader As StreamReader
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
oReader = New StreamReader(OpenFileDialog1.FileName, True)
InformationBox.Text = oReader.ReadToEnd
My.Forms.Home.TextBox5.AppendText(Environment.NewLine & "Opened Customer : " & OpenFileDialog1.FileName & " File")
oReader.Close()
End If
IDBox.Text = ""
Label11.Text = OpenFileDialog1.FileName
End Sub
example of textfile :
Name of customer : Name
Surname of customer :Surname
ID number : 12345678910
Record number : 001
Address of Customer : Address
Can anyone help me please?
In your example code you use StreamReader to read the text file.
The "drawback" of Streams is that you have to manage their disposing manually.
In your example, if an error occurs in oReader.ReadToEnd the line oReader.Close will not be hit and the Stream might remain undisposed thus causing trouble.
So you´d better enclosure your Stream in a Using scope (another option would be to use static System.IO.File.ReadAllLines|ReadAllText methods).
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
'Dim oReader As StreamReader <-- DELETE THIS
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'MY CODE STARTS HERE:
Dim customerInfo As String
Using sr = New StreamReader(OpenFileDialog1.FileName, True)
customerInfo = sr.ReadToEnd()
End Using 'Close the stream early since we have all data needed
'Write all lines into a string array
Dim lines As String() = customerInfo.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
'Get the line where the ID Number is in
Dim idLine As String = lines.Where(Function(l) l.StartsWith("ID number")).FirstOrDefault()
Dim id As String = String.Empty
If Not String.IsNullOrEmpty(idLine) Then
Dim aIdLine() = idLine.Split(":"c) 'Split the ID line by :
If aIdLine.Length >= 1 Then
id = aIdLine(1) 'This should be the actual ID
End If
End If
'Set UI
My.Forms.Home.TextBox5.AppendText(Environment.NewLine & "Opened Customer : " & OpenFileDialog1.FileName & " File")
InformationBox.Text = customerInfo
IDBox.Text = id
Label11.Text = OpenFileDialog1.FileName
End If

Realtime console redirection issue

First of all, I did spend quite some time trying to find an answer to my issue but so far, nothing.
Here is the issue.
I have to run a cli command with arguments and redirect the output (REALTIME) to a textbox.
The command will look like this:
smcli - n name -f "script.scr"
Since smcli is not in the system PATH variable, I first need to go to the right Dir
So, I end up running the full command that looks like this:
cmd /c cd "c:\Program Files(x86)\bla\" && smcli - n name -f "script.scr"
I'll put the code in one sec but basically, the thing is working just fine, I get to the correct directory, run the command, it runs the script, all good.
I tried to redirect the output to a different form but then it doen't work anymore at all; anyway, that would be the bonus question.
The thing is though, when the script runs, it does say step by step what it is doing and sends that info to the output (i.e. restoring the name, restoring the disk config, ...)
As I said, I do get all those outputs....once EVERYTHING is completed (i.e. after 5 minutes of apparent freeze).
NOT redirecting the output to the textbox (i.e. leaving the console window open), I get the output real time.
So here is the code now (there is a bit of it, sorry):
Private Sub ReadMDcfgToolStripButton_Click(sender As Object, e As EventArgs) Handles ReadMDcfgToolStripButton.Click
Dim exitcode As Int32
smcli = Environment.GetEnvironmentVariable("ProgramFiles") & "\Dell\MD Storage Manager\client\" 'SMcli.exe"
Dim gotosmclidir As String = "cd """ & smcli & """"
Dim smclicommand As String
smclicommand = "smcli -n " & mdname & " -c ""save storagearray configuration file=\""" & saveFileDialog2.FileName & "\"" allconfig"";"
cmd = gotosmclidir & " && " & smclicommand
exitcode = RunCommandCom(cmd, "", False)
End Sub
Private Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Int32
' Usage:
'RunCommandCom("DIR", "/W", true)
'For the multiple command on one line the key are the & | && and || command connectors
'•A & B -> execute command A, then execute command B.
'•A | B -> execute command A, and redirect all it's output into the input of command B.
'•A && B -> execute command A, evaluate the errorlevel after running Command A, and if the exit code (errorlevel) is 0, only then execute command B.
'•A || B -> execute Command A, evalutate the exit code of this command and if it's anything but 0, only then execute command B.
TextBox1.Text = "Communication in progress..." & vbCrLf
exitcodelbl.Text = ""
CmdCloseBtn.Enabled = False
ToolStrip1.Enabled = False
CmdOutputPanel.Visible = True
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.FileName = "cmd.exe"
pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
MsgBox(pi.Arguments)
pi.CreateNoWindow = True
pi.UseShellExecute = False
pi.RedirectStandardOutput = True
pi.RedirectStandardError = True
pi.CreateNoWindow = True
p.StartInfo = pi
AddHandler p.OutputDataReceived, AddressOf GotData
p.Start()
p.BeginOutputReadLine()
Do Until p.HasExited
Loop
exitcodelbl.Text = p.ExitCode
Select Case p.ExitCode
Case 0
exitcodelbl.Text += " - Command completed successfully."
Case 1
exitcodelbl.Text += " - Could not communicate with the Array."
Case 9
exitcodelbl.Text += " - Could not communicate with the Array."
Case 14
exitcodelbl.Text += " - Could not communicate with the Array."
Case Else
exitcodelbl.Text += " - Unknown code."
End Select
Return p.ExitCode
End Function
Sub GotData(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
UpdateTextBox(e.Data)
End Sub
Private Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
Private Sub UpdateTextBox(ByVal Tex As String)
If Me.InvokeRequired Then
Dim del As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
Dim args As Object() = {Tex}
Me.Invoke(del, args)
Else
TextBox1.Text &= Tex & Environment.NewLine
End If
End Sub
Apart from the command itself, everything else is comming from researches and they are just put together, the way to run the command on one hand and the redirection on the other hand.
I believe the issue is with my command (looks like that part is VERY sensible).
Either I can have it done a different way (again, it works but not real time) or I missed something trivial...?
Thanks for your help.
Following tinstaafl here is the updated code:
Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
copyrightlbl.Focus()
wait(1000)
Await RunCommandCom("", "192.168.219.152", False) '172.16.1.55
CmdCloseBtn.Enabled = True
End Sub
Private Async Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Task(Of Int32)
TextBox1.Text = "Communication in progress..." & vbCrLf
exitcodelbl.Text = ""
CmdCloseBtn.Enabled = False
ToolStrip1.Enabled = False
CmdOutputPanel.Visible = True
Dim p As Process = New Process()
With p.StartInfo
.WorkingDirectory = Environment.GetEnvironmentVariable("ProgramFiles") & "\Dell\MD Storage Manager\client\" 'Directory for SMcli.exe"
.FileName = "ping.exe"
.Arguments = command + " " + arguments
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
p.Start()
Do Until p.HasExited
TextBox1.Text &= Await p.StandardOutput.ReadLineAsync
Loop
exitcodelbl.Text = p.ExitCode
Return p.ExitCode
End Function
Instead of using a delegate try reading the output directly to the textbox. The StandardOutput property of the Process is a stream that you can use the ReadLineAsync method to get the output. Something like this should work:
pi.CreateNoWindow = True
pi.UseShellExecute = False
pi.RedirectStandardOutput = True
pi.RedirectStandardError = True
p.StartInfo = pi
p.Start()
Do Until p.HasExited
TextBox1.Text &= Await p.StandardOutput.ReadLineAsync
Loop
Intellisense will prompt you to make the sub routine Async, but with that you should get the output you want.
Private Async Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Task(Of Int32)
Await RunCommandCom("", "192.168.219.152", False)

How to run batch commands with dynamic statements?

I am writing code in VB.NET 2.0 and want to run batch commands for FTP using FTP -s:filename command.
I have a Batch file FTP.TXT for FTP Upload. It has the following statements:
OPEN <FPT SERVER IP>
USERNAME
PASSWORD
ASC
CD FOLDERNAME
PUT D:\DRFT000009.TXT FTPDRFTIN.DRFT000009
BYE
I have to dynamically change the filename in the Batch File. Now either I can create a Batch file at runtime and then read it or I got a code to set the input stream of the Process object. But its not working as desired.
This code runs fine but here I read a static batch file FTP.TXT from the computer:
Public Sub FTP4()
Dim psi As ProcessStartInfo
Dim totalerror As String = ""
psi = New ProcessStartInfo()
psi.FileName = "FTP.EXE"
psi.Arguments = " -s:D:\FTP.TXT"
psi.RedirectStandardError = True
psi.RedirectStandardOutput = True
psi.CreateNoWindow = True
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.UseShellExecute = False
Dim process As Process = process.Start(psi)
Dim error2 As String = process.StandardError.ReadToEnd()
totalerror = totalerror & error2
process.WaitForExit()
Response.Write(totalerror)
End Sub
But I want somehow to get the FTP done with custom file name for each request. This is what I tried which is not working:
Public Sub FTP5()
Dim totalerror As String = ""
Dim BatchScriptLines(6) As String
Dim process As New Process
process.StartInfo.FileName = "FTP.EXE"
process.StartInfo.UseShellExecute = False
process.StartInfo.CreateNoWindow = True
process.StartInfo.RedirectStandardInput = True
process.StartInfo.RedirectStandardOutput = True
process.StartInfo.RedirectStandardError = True
process.Start()
process.BeginOutputReadLine()
Using InputStream As System.IO.StreamWriter = process.StandardInput
InputStream.AutoFlush = True
BatchScriptLines(0) = "OPEN <FPT IP ADDRESS>"
BatchScriptLines(1) = "USERNAME"
BatchScriptLines(2) = "PASSWORD"
BatchScriptLines(3) = "ASC"
BatchScriptLines(4) = "CD SFCD40DAT"
BatchScriptLines(5) = "PUT D:\DRFT000006.TXT FTPDRFTIN.DRFT000006"
BatchScriptLines(6) = "BYE"
For Each ScriptLine As String In BatchScriptLines
InputStream.Write(ScriptLine & vbCrLf)
Next
End Using
Dim error2 As String = process.StandardError.ReadToEnd()
totalerror = totalerror & error2
process.WaitForExit()
Response.Write(totalerror)
End Sub
Please advise how I can get the "FTP -s:filename" command executed in this case. Basically I want to do something similar to single line batch file execution which I not able to do.
Being a simple text file with a clear format, you could rewrite the file passing the parameters that need to be dynamically changed
Public Sub FTP4()
' Of course I assume that, at this point your program knows the exact values '
' to pass at the procedure that rebuilds the file'
PrepareFTPFile("D:\DRFT000006.TXT", "USERNAME", "PASSWORD")
' Now you call the original code.....
Dim psi As ProcessStartInfo
Dim totalerror As String = ""
psi = New ProcessStartInfo()
psi.FileName = "FTP.EXE"
psi.Arguments = " -s:D:\FTP.TXT"
....
End Sub
Public Sub PrepareFTPFile(fileToUpload as String, username as string, userpass as string)
Using sw = new StreamWriter("D:\FTP.TXT", False)
sw.WriteLine("OPEN <FPT IP ADDRESS>")
sw.WriteLine(username)
sw.WriteLine(userpass)
sw.WriteLine("ASC")
sw.WriteLine("CD SFCD40DAT")
sw.WriteLine("PUT " + fileToUpload + " FTPDRFTIN." + Path.GetFileNameWithoutExtension(fileToUpdload))
sw.WriteLine("BYE")
End Using
End Sub

VB.NET can't run shadow command in command prompt

I am trying to run the shadow command in cmd.exe from my VB program but for some reason it will not run the command I have tried a few different things all of which have not worked. I was able to save the command to a batch file and then execute it with success but I would prefer to pass the argument/command to command prompt directly and execute this way. Basically, I have a the user run another program I created to extract sessionid and server name (I then just take the server number off the end). They get a 4 digit passcode on their end that is essentially first two is sessionid and last two are server number (all our servers are named ie smdts-(a number) so I just care about what server number they are on) I then take the four digit code and plug it into my shadow admin program. Here is my current code that doesn't work:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SessId As String
Dim PassCode As String
Dim ServNum As String
Dim Prc As Process
PassCode = TextBox2.Text
SessId = PassCode.Substring(0, 2)
ServNum = PassCode.Substring(PassCode.Length - 2)
Dim fileargs As String = " shadow" + " " & SessId + " " + "/server:smdts-" + ServNum
Dim Pinfo = New System.Diagnostics.ProcessStartInfo
Pinfo.FileName = "cmd.exe"
Pinfo.Arguments = fileargs
Pinfo.ErrorDialog = False
Pinfo.UseShellExecute = False
Pinfo.CreateNoWindow = False
Pinfo.WindowStyle = ProcessWindowStyle.Normal
Pinfo.RedirectStandardOutput = False
Pinfo.RedirectStandardInput = True
Pinfo.RedirectStandardError = False
Prc = New Process
Prc.StartInfo = Pinfo
Prc.Start()
End Sub
What does work (batch file which I don't want to use):
Public Class Form1
Public pathvar As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Dim SessId As String
Dim PassCode As String
Dim ServNum As String
PassCode = TextBox2.Text
SessId = PassCode.Substring(0, 2)
ServNum = PassCode.Substring(PassCode.Length - 2)
Dim fileargs As String = " shadow" + " " & SessId + " " + "/server:smdts-" + ServNum
Dim Streamwriter As StreamWriter
Streamwriter = File.CreateText(pathvar + "\ShadowBatch.bat")
Streamwriter.WriteLine(fileargs)
Streamwriter.Close()
Shell(pathvar + "\ShadowBatch.bat")
End Sub
Any help on why the first example is not working would be GREATLY appreciated! Thanks!
Tom K
You're passing shadow ... as the arguments to CMD.
CMD does not support that.
Instead, you need to pass /c shadow ..., which will tell CMD to execute that command and exit.
Alternatively you could run shadow directly, without going through CMD.