how to search for a string in text file

gopi09_m

New Member
Joined
May 18, 2008
Messages
6
hi

i need the code for searching a string in a text files using hte VBA macros.

Regards
krrishna
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Welcome to the board.

Generally speaking, the more information you give us, the more accurately we can fulfill your exact requirements, however this will do what you asked:-
Code:
[FONT=Fixedsys]Option Explicit[/FONT]
 
[FONT=Fixedsys]Public Sub FindStringInTextFile()[/FONT]
 
[FONT=Fixedsys]  Dim intFH As Integer[/FONT]
[FONT=Fixedsys]  Dim sRecord As String[/FONT]
 
[FONT=Fixedsys]  Close[/FONT]
[FONT=Fixedsys]  intFH = FreeFile()[/FONT]
[FONT=Fixedsys]  Open "[COLOR=red]c:\temp\temp.txt[/COLOR]" For Input As #intFH[/FONT]
 
[FONT=Fixedsys]  Do Until EOF(intFH)[/FONT]
[FONT=Fixedsys]    Line Input #intFH, sRecord[/FONT]
[FONT=Fixedsys]    If InStr(sRecord, "[COLOR=red]package[/COLOR]") > 0 Then [/FONT]
[FONT=Fixedsys][COLOR=green]      ' do something[/COLOR][/FONT]
[FONT=Fixedsys][COLOR=black]    End If[/COLOR][/FONT]
[FONT=Fixedsys]  Loop[/FONT]
 
[FONT=Fixedsys]  Close intFH[/FONT]
 
[FONT=Fixedsys]End Sub[/FONT]
Change the bits in red to reflect your file name and search string. Change the bit in green to do whatever you want done when the string is found.
 
Last edited:
Upvote 0
Hi

thanks for the reply.
I will have a folder with 100files(xml ,txt and log files).And i have to search for a particular string in text files in that folder.

Regards
Krrishna
 
Upvote 0
Code:
[COLOR="Blue"]Sub[/COLOR] FindStringInTextFiles()
    
    [COLOR="Blue"]Dim[/COLOR] sLine [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] fso [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Object[/COLOR], aFile [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Object[/COLOR], txtFile [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Object[/COLOR]
    
    [COLOR="Blue"]Set[/COLOR] fso = CreateObject("Scripting.FileSystemObject")

    [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] aFile [COLOR="Blue"]In[/COLOR] fso.GetFolder("C:\[COLOR="Blue"]Text[/COLOR] Files\").Files
        [COLOR="Blue"]If[/COLOR] fso.GetExtensionName(aFile.Name) [COLOR="Blue"]Like[/COLOR] "[Tt][Xx][Tt]*" [COLOR="Blue"]Then[/COLOR]
            [COLOR="Blue"]Set[/COLOR] txtFile = fso.OpenTextFile(aFile.Path)
            [COLOR="Blue"]While[/COLOR] [COLOR="Blue"]Not[/COLOR] txtFile.AtEndOfStream
                sLine = txtFile.ReadLine
                [COLOR="Blue"]If[/COLOR] InStr(sLine, "SEARCH_STRING") > 0 [COLOR="Blue"]Then[/COLOR]
                    [COLOR="Green"]' Do something....[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Wend[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]Next[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
I will have a folder with 100files(xml ,txt and log files).And i have to search for a particular string in text files in that folder.
I wish you'd said that in the first place!

It looks like Sektor is on the ball - he'll sort you out.
 
Upvote 0
Slight modification. Tests for "txt", "xml" and "log" extensions.
Code:
[COLOR="Blue"]Sub[/COLOR] FindStringInTextFiles()
    
    [COLOR="Blue"]Dim[/COLOR] sLine [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR], sExt [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] fso [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Object[/COLOR], aFile [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Object[/COLOR], txtFile [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Object[/COLOR]
    
    [COLOR="Blue"]Set[/COLOR] fso = CreateObject("Scripting.FileSystemObject")
    [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] aFile [COLOR="Blue"]In[/COLOR] fso.GetFolder("C:\[COLOR="Blue"]Text[/COLOR] Files\").Files
        sExt = fso.GetExtensionName(aFile.Name)
        [COLOR="Blue"]If[/COLOR] sExt [COLOR="Blue"]Like[/COLOR] "[Tt][Xx][Tt]*" [COLOR="Blue"]Or[/COLOR] _
           sExt [COLOR="Blue"]Like[/COLOR] "[Ll][Oo][Gg]*" [COLOR="Blue"]Or[/COLOR] _
           sExt [COLOR="Blue"]Like[/COLOR] "[Xx][Mm][Ll]*" [COLOR="Blue"]Then[/COLOR]
            [COLOR="Blue"]Set[/COLOR] txtFile = fso.OpenTextFile(aFile.Path)
            [COLOR="Blue"]While[/COLOR] [COLOR="Blue"]Not[/COLOR] txtFile.AtEndOfStream
                sLine = txtFile.ReadLine
                [COLOR="Blue"]If[/COLOR] InStr(sLine, "SEARCH_STRING") [COLOR="Blue"]Then[/COLOR]
                    [COLOR="Green"]' Do something[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Wend[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]Next[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top