How to run findstr Command in

I need to use findstr command in 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 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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
Dim Findstring = IO.File.ReadAllText("Your File Path")
Dim Lookfor as string = "hello"
If FindString.Contains(Lookfor) then
'Do something
Msgbox("Found: " & Lookfor)
end if

Open in new window

Imports System.IO

Module Module1

    Sub Main()
        ' Store the line in this String.
        Dim search As String
        Dim infile As String
        'Dim outfile As String

        infile = "z:\test\names.csv"
        search = "Joe"
        ' Create new StreamReader instance with Using block.
        TextFileExample(infile, search)

        ' Write the line we read from "file.txt"
        Console.WriteLine(vbCrLf + "press enter to end")

    End Sub

    Public Sub TextFileExample(ByVal filePath As String, searchtext As String)

        ' Verify that the file exists. 
        If System.IO.File.Exists(filePath) = False Then
            Console.Write("File Not Found: " & filePath)
            Dim Findstring() As String = File.ReadAllLines(filePath)
            Dim s As String
            For Each s In Findstring
                If s.Contains(searchtext) Then
                End If

        End If
    End Sub
    End Module

Open in new window

JOLEEJJAuthor Commented:

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/

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

JOLEEJJAuthor Commented:

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.FileName = "cmd.exe"

        myProcess.StartInfo.Arguments = "/findstr /i " & SearchFile & "> " & outfile

        myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        myProcess.StartInfo.CreateNoWindow = True
        If Not myProcess.HasExited Then
        End If
        'MessageBox.Show("The command window was " & _
        '    "closed at: " & myProcess.ExitTime & "." & System.Environment.NewLine & _
        '    "Exit Code: " & myProcess.ExitCode)

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")
JOLEEJJAuthor Commented:
Hi Ark,
It creates a blank JOE.txt without any searched result.  It seems like the Findstr is still not working.

>>It creates a blank JOE.txt<<
I've added /c switch to your code. Did you correct missing space before C:\A\JOETEXT.txt?
Correct arguments: proc.Arguments = ("/c findstr /i """ & strItem & """ C:\A\JOETEXT.txt > C:\A\JOE.txt")
Here is my working code:
   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim strItem = "Personal"
        Using p As New Process
            With p.StartInfo
                .UseShellExecute = False
                .FileName = "cmd.exe"
                .Arguments = "/c findstr /i """ & strItem & """ C:\chkTest.html > C:\findstrTest.txt"
                .CreateNoWindow = True
            End With
        End Using
    End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JOLEEJJAuthor Commented:
Great.  Thanks for your help. It works as I wanted....:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.