[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]
[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]
I wish you'd said that in the first place!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.
[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]