Joseph Jones
asked on
How to run findstr Command in vb.net?
Hi
I need to use findstr command in vb.net windows application. I have tried the following code but it doesn't work. I need to search a string from a text file and copy the result to another text file. This works well in command prompt but not in vb.net windows.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim strItem As String = "ABC78611"
Dim proc As ProcessStartInfo = New ProcessStartInfo
proc.FileName = ("cmd.exe")
proc.Arguments = ("findstr /i """ & strItem & """C:\A\JOETEXT.txt > C:\A\JOE.txt")
Dim pr As Process
proc.WindowStyle = ProcessWindowStyle.Hidden
pr = Process.Start(proc)
End Sub
I need to use findstr command in vb.net windows application. I have tried the following code but it doesn't work. I need to search a string from a text file and copy the result to another text file. This works well in command prompt but not in vb.net windows.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim strItem As String = "ABC78611"
Dim proc As ProcessStartInfo = New ProcessStartInfo
proc.FileName = ("cmd.exe")
proc.Arguments = ("findstr /i """ & strItem & """C:\A\JOETEXT.txt > C:\A\JOE.txt")
Dim pr As Process
proc.WindowStyle = ProcessWindowStyle.Hidden
pr = Process.Start(proc)
End Sub
ASKER
Hi,
I need to search a text within a text file which as 32 columns and more than 12 million rows. The best option I believe to do is using the "Findstr" command prompt. But I need to create a GUI to use this command. Please help me to run this command in VB.NET/
Thanks
Joe
I need to search a text within a text file which as 32 columns and more than 12 million rows. The best option I believe to do is using the "Findstr" command prompt. But I need to create a GUI to use this command. Please help me to run this command in VB.NET/
Thanks
Joe
Check the "Controlling process input/output." section at following
http://www.thescarms.com/dotnet/Process.aspx
Also
http://www.codeproject.com/Articles/170017/Solving-Problems-of-Monitoring-Standard-Output-and
http://www.thescarms.com/dotnet/Process.aspx
Also
http://www.codeproject.com/Articles/170017/Solving-Problems-of-Monitoring-Standard-Output-and
ASKER
Hi,
I followed the above link and I still have problem. Following is my code.
Dim myProcess As Process = New Process()
Dim s As String = "ABC78611"
Dim outfile As String = "C:\A\Joetext.txt"
Dim SearchFile As String = "C:\A\joe.txt"
myProcess.StartInfo.FileNa me = "cmd.exe"
myProcess.StartInfo.Argume nts = "/findstr /i " & SearchFile & "> " & outfile
myProcess.StartInfo.Window Style = ProcessWindowStyle.Hidden
myProcess.StartInfo.Create NoWindow = True
myProcess.Start()
myProcess.WaitForExit(1000 )
If Not myProcess.HasExited Then
myProcess.Kill()
End If
'MessageBox.Show("The command window was " & _
' "closed at: " & myProcess.ExitTime & "." & System.Environment.NewLine & _
' "Exit Code: " & myProcess.ExitCode)
myProcess.Close()
I followed the above link and I still have problem. Following is my code.
Dim myProcess As Process = New Process()
Dim s As String = "ABC78611"
Dim outfile As String = "C:\A\Joetext.txt"
Dim SearchFile As String = "C:\A\joe.txt"
myProcess.StartInfo.FileNa
myProcess.StartInfo.Argume
myProcess.StartInfo.Window
myProcess.StartInfo.Create
myProcess.Start()
myProcess.WaitForExit(1000
If Not myProcess.HasExited Then
myProcess.Kill()
End If
'MessageBox.Show("The command window was " & _
' "closed at: " & myProcess.ExitTime & "." & System.Environment.NewLine
' "Exit Code: " & myProcess.ExitCode)
myProcess.Close()
What is the problem? You are not redirecting the output from the console to your program.
You must use /c switch to execute command via cmd:
proc.Arguments = ("/c findstr /i """ & strItem & """C:\A\JOETEXT.txt > C:\A\JOE.txt")
proc.Arguments = ("/c findstr /i """ & strItem & """C:\A\JOETEXT.txt > C:\A\JOE.txt")
ASKER
Hi Ark,
It creates a blank JOE.txt without any searched result. It seems like the Findstr is still not working.
Joe
It creates a blank JOE.txt without any searched result. It seems like the Findstr is still not working.
Joe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great. Thanks for your help. It works as I wanted....:)
Open in new window
Open in new window