Hello. I was looking for assistance with an issue Im having with a script trying to read strings in a text file. Shown below is the script. Basically, it's looking for the existence of the filenames listed in the array, writes the file names that are found into a text file. Then the script opens the text file with the file names, and if it finds the particular string in the file, writes a cooresponding number to another text file. Then the script reads the numbers in that file and sets a variable to the numbers read, then uses that variable as the exit code. I'm using this exit code for reporting in Altiris Notification Server. Everything appeared to work fine, but then I noticed it would inconsistantly work or not work. For example in the script, the first string to search is Test1.exe. Even though the file is on the systems and the first part of the script writes this to the logfile, my InStr function isn't working and not reading this from the file. Is there a way to have my string searches work consistantly? Let me know if you can assist. Thank you.
ON ERROR RESUME NEXT
strComputer = "."
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("LogFile.txt")
Set objRS = objFSO.CreateTextFile("RSFile.txt")
arrFileNames = Array("'Test1'","'Test2'","'Test3'","'Test4'")
strQueryFilter = "FileName = " & Join(arrFileNames, " OR FileName = ")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("SELECT * FROM CIM_DataFile WHERE " & strQueryFilter,, 48)
For Each objFile In colFiles
objLogFile.write(objFile.FileName & "." & objFile.Extension & vbCrLf)
Next
objLogFile.close
Set objLogFile = objFSO.OpenTextFile("LogFile.txt", 1)
Do Until objLogFile.AtEndOfStream
strSearchThis = strSearchThis & objLogFile.ReadLine
loop
if instr(strSearchThis, "Test1.exe") > 0 then
objRS.Write "1"
end if
if instr(strSearchThis, "test2.dll") > 0 Then
objRS.Write "2"
end if
if instr(strSearchThis, "test3.ocx") > 0 Then
objRS.Write "3"
end if
if instr(strSearchThis, "test4.exe") > 0 Then
objRS.Write "4"
end if
objLogFile.close
objRS.close
Set objRS = objFSO.OpenTextFile("RSFile.txt", 1)
Do until objRS.AtEndOfStream
strResult = strResult & objRS.ReadLine
loop
objRS.Close
If strResult = "" Then
wscript.quit(0)
Else
wscript.quit(strResult)
End if
by: BrandonGalderisiPosted on 2008-09-08 at 08:03:20ID: 22418064
Ok. I'm looking at this and I have a few suggestions:
Use regular expression testing instead of instr.
Use readall instead of readline.
Why write to file, just to read it?
I'm having a little trouble interpreting exactly what you are attempting to accomplish, but it looks like you were reading from a file, searching the file for 4 words, then opening the file to read and stream the results into a single value (strResult).
Does this work for you?
Select allOpen in new window