Jump to content

vbscript to find a string value in a text file and then write an answer


Recommended Posts

Posted

hey

 

no errors but is now putting no at the end of all the lines again

 

when i run the script with WScript.Echo srtContents just after rdFile.Close i get his display

 

 

vbserror3.jpg

 

 

to me it might only be looking at that line see below the full script

 

 

Dim FSO, LogFile, rdFile

 

Set FSO = CreateObject ("Scripting.FileSystemObject")

 

objStartFolder = "\\server\packageinstalllogs$"

 

Set LogFile = FSO.CreateTextFile("\\server\packageinstalllogs$\packageinstallcheckv8.txt",True)

Set objFolder = FSO.GetFolder(objStartFolder)

 

LogFile.writeLine objFolder.Path

 

Set colFiles = objFolder.Files

For Each objFile in colFiles

LogFile.writeLine objFile.Name

Next

 

LogFile.writeLine Echo

 

ShowSubfolders FSO.GetFolder(objStartFolder)

 

 

Sub ShowSubFolders(Folder)

For Each Subfolder in Folder.SubFolders

LogFile.writeLine Subfolder.Path

 

Set objFolder = FSO.GetFolder(Subfolder.Path)

Set colFiles = objFolder.Files

 

For Each objFile in colFiles

LogFile.Write objFile.Name

 

Set rdFile = FSO.OpenTextFile(objFile.Path, 1, TristateFalse, -1)

bFound = False

Do Until rdFile.AtEndOfStream

srtContents = rdFile.ReadLine

If InStr(1, strContents, "success or error status: 0", 1) > 0 Then

bFound = True

Exit Do

End If

Loop

rdFile.Close

 

if bFound = True Then

LogFile.Write ", Yes" & Chr(10)

LogFile.WriteLine Echo

else

LogFile.Write ", No" & Chr(10)

LogFile.WriteLine Echo

end if

Next

LogFile.writeLine Echo

ShowSubFolders Subfolder

Next

End Sub

Posted

It will definitely read each line one by one and check it, the reason only that line shows is that's the line that remains in memory.

 

The last thing I can think to change in the code then is this line:

 

Change this:
If InStr(1, strContents, "success or error status: 0", 1) > 0 Then

To this:
If InStr(1, strContents, "success or error status: 0", 0) > 0 Then

Posted
Ahem

dir > myfile.txt

 

Takes the output of dir and re-routes it to the file myfile.txt instead of outputting it to the screen.

 

and can go down through the directory structure for you as well :-D

 

dir /s /w /p

 

This would list all the files and directories in the current directory and the sub directories after that, in wide format and one page at a time.

 

dir /on

 

List the files in alphabetical order by the names of the files.

 

It doesn't check inside each text file for a certain string and add the results to your output though ;)

Posted
@NETKILLER - I'm sorry but I have to put my hands up and say I'm baffled :confused: the first version I posted worked fine for me, and with each tweak we've made throughout the thread it's continued to work for me, seems there's something odd going on at your end :confused:
Posted
can you port the full script that you are using in case i have mistyped something

 

No problem, the final script I have after all our modifications is this one:

 

Dim FSO, LogFile, rdFile
Dim bFound as boolean

Set FSO = CreateObject ("Scripting.FileSystemObject")

objStartFolder = "C:\test\"

Set LogFile = FSO.CreateTextFile("C:\test\packageinstallcheck.txt",True)
Set objFolder = FSO.GetFolder(objStartFolder)

LogFile.writeLine objFolder.Path

Set colFiles = objFolder.Files
For Each objFile in colFiles
LogFile.writeLine objFile.Name
Next

LogFile.writeLine Echo

ShowSubfolders FSO.GetFolder(objStartFolder)


Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
	LogFile.writeLine Subfolder.Path
	
	Set objFolder = FSO.GetFolder(Subfolder.Path)
	Set colFiles = objFolder.Files
	
	For Each objFile in colFiles
		LogFile.Write objFile.Name
		
		Set rdFile = FSO.OpenTextFile(objFile.Path, 1)
		bFound = False
		Do Until rdFile.AtEndOfStream
			srtContents = rdFile.ReadLine
			if InStr(1, strContents, "success or error status: 0", 0) > 0 Then 
				bFound = True
				Exit Do
			end if
		Loop
		rdFile.Close
		
		if bFound = True Then 
			LogFile.Write ", Yes" & Chr(10)
			LogFile.WriteLine Echo
		else
			LogFile.Write ", No" & Chr(10)
			LogFile.WriteLine Echo
		end if
	Next
	
	LogFile.writeLine Echo
	ShowSubFolders Subfolder
Next
End Sub

 

EDIT: please remember to change the file paths at the start of that script, they are the folders I set up on my machine for testing

Posted

doesn't seem to like this line

 

Dim bFound as boolean

 

(2, 12) Microsoft VBScript compilation error: Expected end of statement

 

I have all the luck........

Posted
doesn't seem to like this line

 

Dim bFound as boolean

 

(2, 12) Microsoft VBScript compilation error: Expected end of statement

 

I have all the luck........

 

No such thing as boolean in this script, Change it to: Dim bFound

 

Steve

  • Thanks 1
Posted
I'm completely stumped, that line is crashing out the script on my machine too. I've omitted it and now it returns "no" for all files for me too... I'm sure I tested it earlier... the "ReadAll" method definitely worked for me earlier, I know I tested that one...
Posted

That’s it i must be jinxed, have made that change and Just done the WScript.Echo srtContents test

and

It has gone back to saying no at the end of each line and now getting a blank message box again.

 

That’s it I am going to go home having the rest of last nights pizza and having a look again after food

Posted
I'm completely stumped, that line is crashing out the script on my machine too. I've omitted it and now it returns "no" for all files for me too... I'm sure I tested it earlier... the "ReadAll" method definitely worked for me earlier, I know I tested that one...

 

And now not even that version of the script is working for me :doh: sorry!

  • Thanks 1
Posted

Hey Steve21 and LosOjos

 

good news i have got the script working, i am running some final test and adding come comments once it is done i will post it thank you both for you help

  • 2 weeks later...
Posted

please find below the script that will search in files for a string value in folder and its sub folders

 

Dim FSO, LogFile, rdFile

Dim bFound, srtContents, installed

 

Set FSO = CreateObject ("Scripting.FileSystemObject")

 

'This part of the code will get the current date and time this

'will be used for adding to the log file name so to create a unique name

'So that the name can conform to windows name convention

'it will then proceed to remove the / in the date and then the : in the time

'this will then leave you with the following DDMMYYYY HHMMSS

 

'start of date and time formatting

 

'gets the current system time and date

datentime = Now()

'removes the / from the date part of the string

dateformated = Replace(datentime, "/", "")

'removes the : from the time part of the string

datentimeformated = Replace(dateformated, ":", "")

 

'end of date and time formatting

 

'set the server and package log locations here

'set server

server = "server name"

'set package log location

packagelogs = "packageinstalllogs$"

 

 

'this uses the preset server and package log location from above

objStartFolder = "\\" & server & "\" & packagelogs

 

'set the location of the applogchecker log file

'

applogcheckerlocation = "appdeployment\applogchecker"

 

Set LogFile = FSO.CreateTextFile("\\" & server & "\" & applogcheckerlocation & "\appinstallcheckerlog" & " " & datentimeformated & ".txt",True)

Set objFolder = FSO.GetFolder(objStartFolder)

 

LogFile.writeLine objFolder.Path

 

Set colFiles = objFolder.Files

For Each objFile in colFiles

LogFile.writeLine objFile.Name

Next

 

LogFile.writeLine Echo

 

ShowSubfolders FSO.GetFolder(objStartFolder)

 

Sub ShowSubFolders(Folder)

For Each Subfolder in Folder.SubFolders

LogFile.writeLine Subfolder.Path

 

Set objFolder = FSO.GetFolder(Subfolder.Path)

Set colFiles = objFolder.Files

 

For Each objFile in colFiles

LogFile.Write Subfolder.Path & objFile.Name

Set rdFile = FSO.OpenTextFile(objFile.Path, 1, TristateFalse, -1)

bFound = "False"

installed = "success or error status: 0."

Do Until rdFile.AtEndOfStream

srtContents = rdFile.ReadLine

If inStr(1, srtContents, installed) > 0 Then

bFound = "True"

Exit Do

end If

Loop

rdFile.Close

 

if bFound = "True" Then

LogFile.Write ", Yes" & Chr(10)

LogFile.WriteLine Echo

else

LogFile.Write ", No" & Chr(10)

LogFile.WriteLine Echo

end if

Next

LogFile.writeLine Echo

ShowSubFolders Subfolder

Next

End Sub

WScript.Echo "log complete"

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...