# vbs script (or similar) to read text files and extract data - from a directory



## pvo01

Hello,

I have a few text files located under my c drive as follow >> c:\text_files\computerA.txt, computerB.txt, computerC.txt, ......

I would like to have a script that can read all of these text files line-by-line in that directory and LOOK for the word "critical" and "rebuilding". 

If those two words does not appares in any of the text file, then write me a new text file called "not_found.txt" and put in the "c:\text_file\result\" folder and tell me that "no critical or rebuilding" was found in that not_found.txt file. And, email me that not_found.txt file.

Whereas, if any of the words were found in any of the text files, then write me a new text file called "word_found.txt" and write the computer_name (eg. computerA, computerB...) into that word_found.txt file. And, email me that word_found.txt file. In this way, I will take a look at the computer_name.txt file my self and look for those word to troubleshoot.


Thanks.


----------



## pvo01

i have a test script as follow; but it doesn't work yet- can someone help me fix it

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

'create a new text file
Set word_found = objFSO.CreateTextFile("c:\spring 2009\word_found.txt", true) 

Set objTextFile = objFSO.OpenTextFile _
("c:\spring 2009\test.txt", ForReading)

strData = objTextFile.ReadAll
objTextFile.Close

'split the text file into lines, and also into words
arrLines = Split(strData, vbCrLf) 
arrWords = Split(strData, vbCrLf)

For Each arrWord in arrLines

'check words
if arrWord = critical then

'set arrword = arrLine

'write the line that has the word critical into the new text file
word_found.WriteLine arrword 'this doens't write the line to the new text file, i need fix

'check for the word remove- if exist, then write this line into the new text, without destroying what is already in that file 
else if arrWord = remove then
word_found.WriteLine arrword 'this doesn't work for me, i need new fix
'the new text file appares as empty file after all. 

end if
end if
next


----------

here is an example of the test.txt file


fan ok
cpu ok
ram critical
chassis remove
...


Thanks.


----------



## Stu_computer

This might be more versatile and easier to work with...

vb script snippet


Code:


dim shell
set shell=createobject("wscript.shell")
errTrap = shell.run("[B]word_found.CMD[/B]", 1, True)
set shell=nothing

then make batch file *word_found.CMD*


Code:


FINDSTR /i /n "critical rebuilding" computer*.txt > c:\text_file\result\word_found.txt

:: result... filename, line number, matching text


----------



## TheOutcaste

This will search the specified file for critical or rebuilding and output the message to the appropriate file name. It's not case sensitive, and the words can't be part of another word unless it's hyphenated. So it will find non-critical, but won't find noncritical or criticalprocess

You can use a collection of files and then loop through the collection and output the file name that contains one of the words. You'd need to open the output file for appending in for the 2nd and subsequent matches


Code:


Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
' Delete output files if they exist
If objFSO.FileExists("C:\Scripts\Results\word_found.txt") Then objFSO.DeleteFile("C:\Scripts\Results\word_found.txt")
If objFSO.FileExists("C:\Scripts\Results\not_found.txt") Then objFSO.DeleteFile("C:\Scripts\Results\not_found.txt")

' Set search words
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.Pattern = "\W{1,}critical\W{1,}|\W{1,}rebuilding\W{1,}"

' Open a file
StrFileName = "C:\Scripts\Test.txt"
Set objFile = objFSO.OpenTextFile(StrFileName,ForReading)
strContents = objFile.ReadAll
objFile.Close

' Search for the words
Set colMatches = objRegEx.Execute(strContents)  

If colMatches.Count > 0 Then
    Set objOutputFile = objFSO.CreateTextFile("C:\Scripts\Results\word_found.txt")
    objOutputFile.WriteLine "Found critical or rebuilding in " & StrFileName
Else
    Set objOutputFile = objFSO.CreateTextFile("C:\Scripts\Results\not_found.txt")
    objOutputFile.WriteLine "Did not find critical or rebuilding in " & StrFileName
End If


----------

