Link to home
Start Free TrialLog in
Avatar of Joseph Jones
Joseph JonesFlag for Australia

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
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

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

Avatar of Joseph Jones

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

Joe
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great.  Thanks for your help. It works as I wanted....:)