Hi All,
I' trying to find an specific word inside a file .I'd like to give my script
a wild card(*.txt) and a word or phrase in the file (exactly like what
widows dose) and find the files on my computer.Is it possible to do it in vb
script?

Thanks

Re: How to seach for a text inside a file by Jerold

Jerold
Fri Mar 11 14:11:31 CST 2005

On Fri, 11 Mar 2005 10:02:39 -0800, "RayAll" <RayAll@microsft.com> wrote:

>Hi All,
>I' trying to find an specific word inside a file .I'd like to give my script
>a wild card(*.txt) and a word or phrase in the file (exactly like what
>widows dose) and find the files on my computer.Is it possible to do it in vb
>script?
>
>Thanks
>
Probably, but this is very easy using a .bat file.
Syntax:

MySearch File String

where File is the file path, wildcards allowed, and String is the seach string, like "Jack and Jill went up the hill"
MySearch.bat contains:

@echo off
if {%2}=={} @echo Syntax: MySearch File String&goto :EOF
setlocal
set file=%1
set string=%2
set string=%string:"=%
for /f "Tokens=*" %%f in ('dir %file% /B /S') do (
for /f "Tokens=*" %%t in ('type "%%f"^|FINDSTR /I /L /C:"%string%"') do (
@echo "%%f"
)
)
endlocal


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com

Re: How to seach for a text inside a file by RayAll

RayAll
Fri Mar 11 14:48:34 CST 2005

How should I return its value from the script? name of the files that it
found.

Thanks


"Jerold Schulman" <Jerry@jsiinc.com> wrote in message
news:d2u331dva7alc181bthnptn5gbvov749oa@4ax.com...
> On Fri, 11 Mar 2005 10:02:39 -0800, "RayAll" <RayAll@microsft.com> wrote:
>
>>Hi All,
>>I' trying to find an specific word inside a file .I'd like to give my
>>script
>>a wild card(*.txt) and a word or phrase in the file (exactly like what
>>widows dose) and find the files on my computer.Is it possible to do it in
>>vb
>>script?
>>
>>Thanks
>>
> Probably, but this is very easy using a .bat file.
> Syntax:
>
> MySearch File String
>
> where File is the file path, wildcards allowed, and String is the seach
> string, like "Jack and Jill went up the hill"
> MySearch.bat contains:
>
> @echo off
> if {%2}=={} @echo Syntax: MySearch File String&goto :EOF
> setlocal
> set file=%1
> set string=%2
> set string=%string:"=%
> for /f "Tokens=*" %%f in ('dir %file% /B /S') do (
> for /f "Tokens=*" %%t in ('type "%%f"^|FINDSTR /I /L /C:"%string%"') do (
> @echo "%%f"
> )
> )
> endlocal
>
>
> Jerold Schulman
> Windows Server MVP
> JSI, Inc.
> http://www.jsiinc.com



Re: Re: How to seach for a text inside a file by Jerold

Jerold
Sat Mar 12 07:00:54 CST 2005


In keeping with the batch script theme:

for /f "Tokens=*" %%f in ('mySearch File "String"') do (
call :DoSomeThing %%f
)

or

setlocal ENABLEDELAYEDEXPANSION
for /f "Tokens=*" %%f in ('mySearch File "String"') do (
set filenames=%%f
@echo !filenames!
)

See tip 8204 in the 'Tips & Tricks' at http://www.jsiinc.com for another approach.


On Fri, 11 Mar 2005 12:48:34 -0800, "RayAll" <RayAll@microsft.com> wrote:

>How should I return its value from the script? name of the files that it
>found.
>
>Thanks
>
>
>"Jerold Schulman" <Jerry@jsiinc.com> wrote in message
>news:d2u331dva7alc181bthnptn5gbvov749oa@4ax.com...
>> On Fri, 11 Mar 2005 10:02:39 -0800, "RayAll" <RayAll@microsft.com> wrote:
>>
>>>Hi All,
>>>I' trying to find an specific word inside a file .I'd like to give my
>>>script
>>>a wild card(*.txt) and a word or phrase in the file (exactly like what
>>>widows dose) and find the files on my computer.Is it possible to do it in
>>>vb
>>>script?
>>>
>>>Thanks
>>>
>> Probably, but this is very easy using a .bat file.
>> Syntax:
>>
>> MySearch File String
>>
>> where File is the file path, wildcards allowed, and String is the seach
>> string, like "Jack and Jill went up the hill"
>> MySearch.bat contains:
>>
>> @echo off
>> if {%2}=={} @echo Syntax: MySearch File String&goto :EOF
>> setlocal
>> set file=%1
>> set string=%2
>> set string=%string:"=%
>> for /f "Tokens=*" %%f in ('dir %file% /B /S') do (
>> for /f "Tokens=*" %%t in ('type "%%f"^|FINDSTR /I /L /C:"%string%"') do (
>> @echo "%%f"
>> )
>> )
>> endlocal
>>
>>
>> Jerold Schulman
>> Windows Server MVP
>> JSI, Inc.
>> http://www.jsiinc.com
>


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com

Re: Re: How to seach for a text inside a file by ALI-R

ALI-R
Sun Mar 13 19:40:10 CST 2005

Sorry,but I think I didn't descibe the problem properly,I am trying to call
your batch file from my VBScript code,dose what you sent me actually returns
the name of the files to the caller of the batch code?

My problem now is two things:

1) how to call this batch file from my vb script code
2) how to return the name of the files from batch file to the caller(vb
script code)

Thanks
"Jerold Schulman" <Jerry@jsiinc.com> wrote in message
news:8fp531he24db525kvm8usm3832cvimovtm@4ax.com...
>
> In keeping with the batch script theme:
>
> for /f "Tokens=*" %%f in ('mySearch File "String"') do (
> call :DoSomeThing %%f
> )
>
> or
>
> setlocal ENABLEDELAYEDEXPANSION
> for /f "Tokens=*" %%f in ('mySearch File "String"') do (
> set filenames=%%f
> @echo !filenames!
> )
>
> See tip 8204 in the 'Tips & Tricks' at http://www.jsiinc.com for another
> approach.
>
>
> On Fri, 11 Mar 2005 12:48:34 -0800, "RayAll" <RayAll@microsft.com> wrote:
>
>>How should I return its value from the script? name of the files that it
>>found.
>>
>>Thanks
>>
>>
>>"Jerold Schulman" <Jerry@jsiinc.com> wrote in message
>>news:d2u331dva7alc181bthnptn5gbvov749oa@4ax.com...
>>> On Fri, 11 Mar 2005 10:02:39 -0800, "RayAll" <RayAll@microsft.com>
>>> wrote:
>>>
>>>>Hi All,
>>>>I' trying to find an specific word inside a file .I'd like to give my
>>>>script
>>>>a wild card(*.txt) and a word or phrase in the file (exactly like what
>>>>widows dose) and find the files on my computer.Is it possible to do it
>>>>in
>>>>vb
>>>>script?
>>>>
>>>>Thanks
>>>>
>>> Probably, but this is very easy using a .bat file.
>>> Syntax:
>>>
>>> MySearch File String
>>>
>>> where File is the file path, wildcards allowed, and String is the seach
>>> string, like "Jack and Jill went up the hill"
>>> MySearch.bat contains:
>>>
>>> @echo off
>>> if {%2}=={} @echo Syntax: MySearch File String&goto :EOF
>>> setlocal
>>> set file=%1
>>> set string=%2
>>> set string=%string:"=%
>>> for /f "Tokens=*" %%f in ('dir %file% /B /S') do (
>>> for /f "Tokens=*" %%t in ('type "%%f"^|FINDSTR /I /L /C:"%string%"') do
>>> (
>>> @echo "%%f"
>>> )
>>> )
>>> endlocal
>>>
>>>
>>> Jerold Schulman
>>> Windows Server MVP
>>> JSI, Inc.
>>> http://www.jsiinc.com
>>
>
>
> Jerold Schulman
> Windows Server MVP
> JSI, Inc.
> http://www.jsiinc.com



Re: How to seach for a text inside a file by Torgeir

Torgeir
Mon Mar 14 07:10:49 CST 2005

ALI-R wrote:

> Sorry,but I think I didn't descibe the problem properly,I am trying
> to call your batch file from my VBScript code,dose what you sent me
> actually returns the name of the files to the caller of the batch code?
>
> My problem now is two things:
>
> 1) how to call this batch file from my vb script code
> 2) how to return the name of the files from batch file to the caller
> (vb script code)
Hi

Here you go:

'--------------------8<----------------------

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1


sBatFile = "c:\Scripts\MySearch.bat"
sFilePattern = "c:\tst\*.txt"
sTextToSearchFor = "Jack and Jill went up the hill"

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

If Not oFSO.FileExists(sBatFile) Then
MsgBox "Could not find batch file, quitting!", _
vbCritical + vbSystemModal, "Text search"
WScript.Quit
End If

sBatFileShort = oFSO.GetFile(sBatFile).ShortPath

sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName

oShell.Run "%comspec% /c " & sBatFileShort & " """ & sFilePattern _
& """ """ & sTextToSearchFor & """ >" & sTempFile, 0, True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)

' If text string is found several times in the same file, duplicate
' lines are created in the output file as well, using a dictionary
' object to avoid those duplicates
Set dicFileList = CreateObject("Scripting.Dictionary")
dicFileList.CompareMode = vbTextCompare

Do While fFile.AtEndOfLine <> True
sLine = fFile.ReadLine
If sLine <> "" Then
If Not dicFileList.Exists(sLine) Then
dicFileList.Add sLine, ""
End If
End If
Loop
fFile.Close
oFSO.DeleteFile(sTempFile)

If dicFileList.Count = 0 Then
WScript.Echo "No files found!"
Else
' enumerate the dictionary object, echoing the key value:
For Each sFile In dicFileList
WScript.Echo sFile
Next
End If

'--------------------8<----------------------
--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx