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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search TXT file for a Phrase and run IF THEN statement

Status
Not open for further replies.

mangeek

IS-IT--Management
Jun 20, 2011
3
US
HI all i need a script in VB that will do the following...

Search for phrase "Microsoft Office Proof (English) 2010"
In TXT file "\\servername\directory\software.txt"
If phrase = "Microsoft Office Proof (English) 2010" Then
Stop Script
Else
Run "\\Servername\directory\setup.exe"

Any one have any ideas on how to accomplish this...
Thanks in advance for any help i greatly appreciate it...
 
Not tested, but should be a good way towards what you want:
Code:
Const ForReading = 1
Dim sReadAll, sInputFile, sSearchString
sInputFile = "\\servername\directory\software.txt"
sSearchString = "Microsoft Office Proof (English) 2010"

Dim fso, f, objShell
Set objShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(sInputFile, ForReading)
sReadAll = f.ReadAll

If InStr(sReadAll, sSearchString) > 0 Then
   objshell.run Chr(34) & "\\Servername\directory\setup.exe" & Chr(34)
Else
   wscript.quit
End If
 
Ok that almost works but if you use this then it just install or quits...

So in this statement
If InStr(sReadAll, sSearchString) > 0 Then
objshell.run Chr(34) & "\\Servername\directory\setup.exe" & Chr(34)

The program goes on to install...

If you use this statement
If InStr(sReadAll, sSearchString) > 0 Then
wscript.quit

The program just quits...

it seems to be ignoring the Else Statement and is just using the If Then Statement with no regard to the search... Maybe being > 0 is the wrong way to compare the statement...

Any ideas on how to fix this???

Thanks Again...
 
I don't follow...

The InStr() function will return the position of the search string (second parameter) found inside the string to be searched (first parameter). If the string is not found, it will return a 0.
So, the expression InStr(sReadAll, sSearchString) > 0 will return "True" if sSearchString is found anywhere inside sReadAll.

Try the code below, and see whether the If statement is working as you expect. If it is, replace the echo's with whatever code you want.
Code:
If InStr(sReadAll, sSearchString) > 0 Then 
   wscript.echo "String was found."
Else
   wscript.echo "String was not found."
End If
 
Are you testing with 2 different input files, 1 that is known to contain the search string and the other known to not contain the search string?

So in this statement
If InStr(sReadAll, sSearchString) > 0
Then objshell.run Chr(34) & "\\Servername\directory\setup.exe" & Chr(34)
The program goes on to install...

If you use this statement
If InStr(sReadAll, sSearchString) > 0
Then wscript.quit
The program just quits...

It appears that you are testing with a file that contains the search string and you are editing the logic statements in the script. In the first case the search string is found so it starts the install program; in the 2nd case the search string is found so it quits.

Perhaps you can clarify what you want the script to do. As it stands it, appears to me that the posted script does exactly what you asked for.
 
mangeek: Looking back, it seems I misread your original post... If you only want to run the program if the string is NOT found, the logic is the same, I just got the commands backwards:
Code:
If InStr(sReadAll, sSearchString) > 0 Then
   'Search string was found
   wscript.quit
Else
   'Search string was NOT found
   objshell.run Chr(34) & "\\Servername\directory\setup.exe" & Chr(34)
End If
 
After Playing with it i got it to work... Just had to fix a ID 10 T Error... Apparently i was having a blond moment... Thanks everyone for your help!!! Much Appreciated!!!

BTW, Here is the Finished Code to do a search and install of Microsoft Office 2010...

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("C:\Software.txt")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
("SELECT * FROM Win32_Product")
objTextFile.WriteLine "Caption" & vbtab & _
"Description" & vbtab & "Identifying Number" & vbtab & _
"Install Date" & vbtab & "Install Location" & vbtab & _
"Install State" & vbtab & "Name" & vbtab & _
"Package Cache" & vbtab & "SKU Number" & vbtab & "Vendor" & vbtab _
& "Version"
For Each objSoftware in colSoftware
objTextFile.WriteLine objSoftware.Caption & vbtab & _
objSoftware.Description & vbtab & _
objSoftware.IdentifyingNumber & vbtab & _
objSoftware.InstallLocation & vbtab & _
objSoftware.InstallState & vbtab & _
objSoftware.Name & vbtab & _
objSoftware.PackageCache & vbtab & _
objSoftware.SKUNumber & vbtab & _
objSoftware.Vendor & vbtab & _
objSoftware.Version
Next
objTextFile.Close

Const ForReading = 1
Dim sReadAll, sInputFile, sSearchString
sInputFile = "C:\Software.txt"
sSearchString = "Microsoft Office Proof (English) 2010"

Dim fso, f, objShell
Set objShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso_OpenTextFile(sInputFile, ForReading)
sReadAll = f.ReadAll

If InStr(sReadAll, sSearchString) > 0 Then
wscript.quit
Else
objshell.run Chr(34) & "\\Input Server name\Input Directory\Input Setup File Name" & Chr(34)
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top