Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search word within a file

Status
Not open for further replies.

KimVong

IS-IT--Management
Sep 13, 2002
90
US
Is there a way to use VBScript to find a specific word in a file. For example, I have a folder with five files. file1, file2,file3,file4,file5. And there is a word in file2 that I will search. and it will tell me that this word is in file2. or more than one file.
please help
 
There are 2 ways I can think of to do this. First is with the INSTR command. Second would be with a regular expresion.

I think it is easiest to use INSTR.

Here is a quick example that checks an INI file for the word Instrument.

Code:
ForReading = 1
Set oFSO=CreateObject("Scripting.FileSystemObject")
'open the ini file
Set oTextStream = oFSO.OpenTextFile("X:\win.ini")
'make an array from the ini file
MyList = Split(oTextStream.ReadAll, vbNewLine)
'close the ini file
oTextStream.Close
	
'Now check each line	
	For Each Line In MyList
		'if there is a match, split the line to an array
		If Left(Line,11) = "Instrument=" Then
			SubArray =Split(Line,"=")
			'the end of the array is the value we want
			Instvalue= SubArray(UBound(SubArray))
			WScript.Echo Instvalue
		End If
	
	Next

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Hello KimVong,

This is what have to do. fso.getfile() to get the file. Open it as text stream ofile.openastextstream(). Then readall the context by ots.readall. This give you a string. Then use instr function to test its appearance, the signature string you want to find, within the string, ie, the content. You two modes, binary & text, essentially meaning case-sensitive and -insensitive for ascii file.
[tt]
if instr(1,ots.readall,signature,1)<>0 then
wscript.echo "you've found the signature string"
else
wscript.echo "Signature string is not there."
end if
[/tt]
The whole thing repeated for each file.

regards - tsuji
 
Thank for all your help.
But I am new at this vbs. I can't seem to use the code above. Let's say my folder is in c:\temp
and I want to search in this temp folder for a file that contain the work "test" in the file. How do I code this? I really need help on this.
thanks for all the previous reply

-Kim
 
KimVong,

Try this.
Code:
signature="test"
folderpath="c:\temp"

set fso=createobject("scripting.filesystemobject")
set ofolder=fso.getfolder(folderpath)
for each ofile in ofolder.files
    set ots=ofile.openastextstream(1)
    if instr(1,ots.readall,signature,1)<>0 then
        wscript.echo "you've found the signature string in " & ofile.path
    else
        wscript.echo "Signature string is not there in " & ofile.path
    end if
next
set ofolder=nothing
set fso=nothing
- tsuji
 
hey tsuji
thanks, and it work. I have another questions. Can this program search for word in PDF file? Or this only work in text file. what can I change to make it search in pdf.
your help is greatly appreciated

kim
 
KimVong,

It won't for pdf. Also know the limit of the scheme. It will consider as found with word like "contest" or "testimonial" etc... For a more rigorous filtering, you have to take a look of regex. Nonetheless, the readall part is still valid.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top