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

Going through multiple files in the folder and retrieving information 1

Status
Not open for further replies.

pivich

Technical User
Jan 24, 2007
11
CA
Good day!

I'd really appreciate help from someone who is more experienced than I am.

Problem:
- I have around 500 .txt files in one folder. I need to write the script to open each file. Then go through the text and look for specific words. For example if it has "test" or "password" in it. If it does or doesn't, script has to write true or false in another .txt file(same file for all 500 files). Then it has to close file and go on to the next one, until the output file would have the list of true and false statements for all 500 files.

That information I would use to create a spreadsheet, that's why it would be perfect if outputs for each file would be separated by commas so they can be converted into the columns in excel.

I'm particularly interested in the script that goes trough the files in the folder and searches for specific word and writes into other file.

Thank you for your help!
 
A very similar topic below yours may get you started. It only displays the filenames right now, but it may give you something to start with. Have you put anything together yet?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I've wrote the script that writes on the files. But the whole concept of going through the files in one folder is quite confusing for me and searching the web didin't bring any specific answers.

I've tried to modify the script that displays the name of the files in the folder to actually open those files. But it didn't bring me far:

strDirName = InputBox("Enter the directory")

Set objFileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFileSystem.GetFolder(strDirName)
Set colFiles = objFolder.Files 'returns files in the folder

For Each FileObj in colFiles

Dim FSO, AFile, AFileStream

Set FSO = CreateObject("Scripting.FileSystemObject")
Set AFileStream = FSO.OpenTextFile(strDirName + strSWname,8)

WScript.Echo AFileStream.ReadLine

Next
 
Something like this?

Code:
Dim objFSO, objFolder, colFiles, File, strDirName, objFile, objFile2

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

strDirName = InputBox("Enter the directory")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\output.txt", ForWriting, True)
Set objFolder = objFSO.GetFolder(strDirName)
Set colFiles = objFolder.Files                        'returns files in the folder

For Each File in colFiles
	Set objFile2 = objFSO.OpenTextFile(File.Path, ForReading)
	If InStr(objFile2.ReadAll, "password") Then
		objFile.WriteLine File.Name & ",True"
	Else
		objFile.WriteLine File.Name & ",False"
	End If
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks PHV! Excellent code dm4ever!

I can't figure out how to use it for multiple words. I'm actually looking for certain expressions after specific commands.

The .txt files are switch outputs. I need to check if the commands worked in all of them. So I need a script that looks for commands and after it would find them, it has to look for certain words that would show if the command has worked. SO I have a couple commands to look for. Another problem is that one command is repeating, so script has to ignore first mentioning of that command until it finds 'end' statement. I've tried to use multiple if statements, but it got me nowhere. Another problem is that I have to record the whole line (not True or False statement) after one command.

So in short the script has to look for certain words and when it finds them, it has to look for specific words and write True or False in the output file, depending if it found those expressions after command or not.

I appreciate your help!
 
perhaps you might need to build a list of things to look for and then read the txt files line by line?
 
Do you have an example of a file you would be reading and what you want to look for? Sounds like using a Regular Expression might be useful for this. An example would be good for others to provide feedback.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Sure!

How the file looks like:

*****************************
radomtext
radomtext
radomtext
radomtext
! text
version 11
fefhe#sh run <- first mentioning of the run
random text
t random text end <- end doesn’t count here
random text
end <- end alone on the line, counts. Script can start recording now.
fefhe#config
fefhe (config)#text <-infromation that I need to check
fefhe (config)#text2 <-text2 follows text, script has to send true, if not, sends false to the output file
fefhe#another command
text, random text random text
fefhe#show
line <- copy whole line and send it to the output file

fefhe#sh run <-second mentioning
randomtext
randomtext
!
randomtext
!
text3
text4
<-again, if text4 goes after text3, script sends true
end

This is the structure of the files. The reason first sh run should not be counted is because it is before the actual chages take plase. And only after first mentioning sh run the script should start looking fore needed expressions or words.

Thanks for the help!
 
The following returned the lines you listed in your example. You may need to modify for other possibilities. I'm very new to regular expressions so someone may offer better patterns or perhaps alternate methods to achieve what you want.

Code:
Dim objFSO, objFile, strText, strKeyWord1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\temp\temp.txt", 1)
strText = objFile.ReadAll

strKeyWord1 = "text"
WScript.Echo RegExpMatch(strText, "end\r\n.*#config\r\n.*\(config\)#" & strKeyWord1 & "\r\n.*\(config\)#(.*)(\r\n)")
WScript.Echo RegExpMatch(strText, "#show\r\n(.*)")
strKeyWord1 = "text3"
WScript.Echo RegExpMatch(strText, strKeyWord1 & "\r\n(.*)")

Function RegExpMatch(strTxt, strPattern)
' 	On Error Resume Next
	
	Dim RegEx:Set RegEx = New RegExp
	RegEx.IgnoreCase = True
	RegEx.Pattern = strPattern
	RegExpMatch = RegEx.Execute(strTxt)(0).SubMatches(0)
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks dm4ever!
I tried your script. And due to my lack of knowledge in vbscript and especially regular expressions I can't make your script to work.

It keeps giving me error on 19th line.
" RegExpMatch = RegEx.Execute(strTxt)(0).SubMatches(0) "

What can be a possible error?

Thank you!
 
The error may be returning because there is no match being found matching the patter being used. I'll see if I can find a non-RegExp method for accomplishing this.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hey!

I'm trying to write it without the regular exp. But I have a couple problems:

Set objFolder = objFSO.GetFolder(strDirName)
Set colFiles = objFolder.Files 'returns files in the folder

For Each File in colFiles
Set objFile2 = objFSO.OpenTextFile(File.Path, ForReading)

Call FindBytes(objFile2, output)

Sub FindBytes(objFileforReading, output)
MsgBox "1"
Dim strLine, strFind, Flag, Flag2
Flag = 0
Flag2 = 0

Do Until Flag2 = 1 OR objFileforReading.AtEndOfStream
strLine = objFileforReading.Readline

strFind = "copy startup tftp"

If InStr(1, LCase(strLine),LCase(strFind), VBTEXTCOMPARE)>0 Then
Flag = 1
End If


If Flag = 1 Then

strFind = "bytes copied in"


If InStr(1,LCase(strLine),LCase(strFind), VBTEXTCOMPARE)>0 Then
output = " True"
Flag2 = 1
Else
output = " False"
End If

End If

Loop
MsgBox output
Call FindNtp(objFileforReading, output)

End Sub

For some reason output file doesn't not get transferred to the other Sub. In the next sub I would have output = output &",False" and same for the next ones until in the last sub it would print it to the output

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Artem\test\output2.txt", ForAppending, True) 'output dir

objFile.WriteLine File.Name & output

But the only thing it prints is only one statement, as opposed to collection of true and false from all subs. What can be a problem? I spent hours trying to find the mistake...

Thank you!
 
You need to move your sub outside your current loop.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top