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
JOLEEJJAsked:
Who is Participating?
 
ArkCommented:
>>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
            p.Start()
            p.WaitForExit()
            p.Close()
            MsgBox("Done!")
        End Using
    End Sub

Open in new window

0
 
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")
        Console.ReadLine()

    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)
        Else
            Dim Findstring() As String = File.ReadAllLines(filePath)
            Dim s As String
            For Each s In Findstring
                If s.Contains(searchtext) Then
                    Console.WriteLine(s)
                End If
            Next

        End If
    End Sub
    End Module

Open in new window

0
 
JOLEEJJAuthor Commented:
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
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
CodeCruiserCommented:
0
 
JOLEEJJAuthor Commented:
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.FileName = "cmd.exe"

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

        myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        myProcess.StartInfo.CreateNoWindow = 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()
0
 
CodeCruiserCommented:
What is the problem? You are not redirecting the output from the console to your program.
0
 
ArkCommented:
You must use /c switch to execute command via cmd:
proc.Arguments = ("/c findstr /i """ & strItem & """C:\A\JOETEXT.txt > C:\A\JOE.txt")
0
 
JOLEEJJAuthor Commented:
Hi Ark,
It creates a blank JOE.txt without any searched result.  It seems like the Findstr is still not working.

Joe
0
 
JOLEEJJAuthor Commented:
Great.  Thanks for your help. It works as I wanted....:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.