Searching for a file|Searching for a set of strings in .vbs

June 21, 2012 at 08:22:04
Specs: Windows XP
Hey guys,

This is related to .vbs script. I chose "Batch/Scripting" as the "Now Subcategory" when publishing this question.
I have .vbs script and what i want to do with it is:
1) I want it to look for a file, a log file. I want to make it as dynamic as possible, instead of hard-coding the location and name of the file everytime I have a new log created. So, in this case, I am trying to make it use the location and file names to locate and grab that file.
2) Once, it finds that log file, search the file for a particular set of strings. (I need it to make sure that the execution was successful or not).

Here is what I have so far:

Option Explicit
Dim objFSO, dirName, fileName, objReadFile
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Identifies the file being read. In this case "file directory txt file" for the location name
Set objReadFile = objFSO.OpenTextFile("c:\automatic_deployment\filedir.txt", 1, False)
'Reads the file until the end.
Do Until objReadFile.AtEndOfStream
'Sets the line being read to a variable named strLine.
dirName = objReadFile.ReadLine
'Trims the strLine variable to remove any leading or trailing spaces.
dirName = trim(dirName)

'Identifies the file being read. Now, looking for the name of the file.
Set objReadFile = objFSO.OpenTextFile("c:\automatic_deployment\filename.txt", 1, False)
'Reads the file until the end.
Do Until objReadFile.AtEndOfStream
'Sets the line being read to a variable named strLine.
fileName = objReadFile.ReadLine
'Trims the strLine variable to remove any leading or trailing spaces.
fileName = trim(fileName)

'Set the search condition. We have to find the final log file by using those "dirName" + "fileName" strings.
'Once we do that, then look for errors, in this case, we're looking for "*** RC (return code) = 0"
'in the in that log file.


if instr(?????strLine, "*** RC (return code) = 0") Then ....???


End If
Loop
wscript.echo strLine
set objFSO = nothing
wscript.quit()


As you can see, I am stuck at the part where I am supposed to use the dirName and filename objects/strings and find the file and read it in order to find the string "*** RC (return code) = 0".

something like this, maybe? :
if instr(c:\dirName\fileName_DEV_Log.txt, "*** RC (return code) = 0") Then ....???

Any ideas? Thanks a lot in advance!


See More: Searching for a file|Searching for a set of strings in .vbs

Report •


#1
June 22, 2012 at 21:16:10
I think you would be way better-off to use plain old batch and native commands (ie: findstr), if you can. If that is verboten, then vbscript can do it, but not as easily.
Or, you can meld the two approaches using vbscript's shell, to a batch or commandline with output piped to a listfile of all files that had failures. For findstr, here's the basic: findstr /s /c /m ""*** RC (return code) = 0" *.log > fail
This not only gives you the full path, but scans all files with a given extension for the error. Then you could use vbscript, if you want, to open "fail" and read it and send each line to the appropriate destination. I'm not real clear on your requirements, so I will probly not get all this right... Using straight vbscript, you will have to have a nested loop, outer loop: open list of files, then read each dir+file to get the filename, then innerloop: open the specific file and read through it to search for the error-message. Not a big deal, but still batch makes things much simpler using native windows command "findstr" which cuts through 90 pecent of your task in "one fell stroke". vbscript can "shell" the batch or commandline. (ps: none my business, but you sure RC zero is failure? normally it's the opposite but I'm clueless with sql. Also possible to redirect error output of all sql executions to one file using >> (?) or a sql modifier.

Report •

#2
June 23, 2012 at 14:27:04
super-duper: So, in this case, I am trying to make it use the location . . .
Which location?

super-duper: . . . and file names . . .
What names?

super-duper: . . . to locate and grab that file.
Which file?

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question