![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Reading file names and Searching for text I have written a script which will read the file names from a text file. After getting the file name, it will check for the file type ie whether the file is text file or word file and based on the file type it will perform the text search function. The script works fine, but it is reading only the first 3 names from the text file and it is not iterating thourgh all the filenames. Below is the code. Can anyone help me in identifying where i am going wrong. Rem Creating Regular Expression for pattern match strString = "green|blue" Set objRegExp = CreateObject("VBScript.RegExp") objRegExp.Pattern = strString objRegExp.IgnoreCase = True objRegExp.Global = True Rem Creating Word Application for writing the output Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.Font.Name = "Arial" objSelection.Font.Size = "10" Rem reading for files names from the text file Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("Z:\Script\filenames.txt", ForReading) Rem reading all the filenames from the textfile one by one Do Until objFile.AtEndOfStream filename = objFile.ReadLine WScript.Echo filename Set objFileType = objFSO.GetFile(filename) filetype = objFileType.Type if filetype = "Text Document" Then WScript.Echo "Text File" Rem Searching the text files for pattern match Set objFile = objFSO.OpenTextFile(filename, ForReading) objSelection.TypeText "------------------------------------------" & vbCrLf objSelection.TypeText "File Name -" & filename & vbCrLf & vbCrLf objSelection.TypeText "------------------------------------------" & vbCrLf Rem Search the text file till the end of file Do Until objFile.AtEndOfStream strSearchString = objFile.ReadLine Rem Pattern matching Set colMatches = objRegExp.Execute(strSearchString) If colMatches.Count > 0 Then For Each strMatch in colMatches Rem writing the value to Word file objSelection.TypeText "Match found -" & strSearchString & vbCrLf & vbCrLf Next End If Loop ElseIf filetype = "Microsoft Word Document" Then WScript.Echo "This is a Word file" wordPath = "Z:\Script\"&filename WScript.Echo "Extract Data from " & filename Set objWord1 = CreateObject("Word.Application") objWord1.DisplayAlerts = 0 objWord1.Documents.Open wordPath, false, True objSelection.TypeText "------------------------------------------" & vbCrLf objSelection.TypeText "File Name -" & filename & vbCrLf & vbCrLf objSelection.TypeText "------------------------------------------" & vbCrLf Set objDoc1 = objWord1.ActiveDocument Set colParagraphs = objDoc1.Paragraphs For Each objParagraph In colParagraphs strContents = objParagraph.Range.Text Set colMatches = objRegExp.Execute(strContents) For Each strMatch in colMatches objSelection.TypeText "Match found -" & strContents & vbCrLf & vbCrLf Next Next objDoc1.Close Set objDoc1 = Nothing objWord1.Quit Set objWord1 = Nothing Else objSelection.TypeText "File: " &filename &" is Invalid File Type" End If Loop End if Loop objDoc.SaveAs("Z:\Script\Scan_results.doc") objWord.Quit |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Reading file names and Searching for text "Codeblack" <Codeblack@xxxxxx> wrote in message news:6AE4C3C2-FAFF-449B-BFAD-8334892968C6@xxxxxx Quote: >I have written a script which will read the file names from a text file. > After getting the file name, it will check for the file type ie whether > the > file is text file or word file and based on the file type it will perform > the > text search function. The script works fine, but it is reading only the > first > 3 names from the text file and > it is not iterating thourgh all the filenames. Below is the code. Can > anyone > help me in identifying where i am going wrong. > > > Rem Creating Regular Expression for pattern match > strString = "green|blue" > Set objRegExp = CreateObject("VBScript.RegExp") > objRegExp.Pattern = strString > objRegExp.IgnoreCase = True > objRegExp.Global = True > Rem Creating Word Application for writing the output > Set objWord = CreateObject("Word.Application") > objWord.Visible = True > Set objDoc = objWord.Documents.Add() > Set objSelection = objWord.Selection > objSelection.Font.Name = "Arial" > objSelection.Font.Size = "10" > > Rem reading for files names from the text file > Const ForReading = 1 > Set objFSO = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO.OpenTextFile("Z:\Script\filenames.txt", ForReading) > Rem reading all the filenames from the textfile one by one > Do Until objFile.AtEndOfStream > filename = objFile.ReadLine > WScript.Echo filename > Set objFileType = objFSO.GetFile(filename) > filetype = objFileType.Type > > if filetype = "Text Document" Then > > WScript.Echo "Text File" > Rem Searching the text files for pattern match > Set objFile = objFSO.OpenTextFile(filename, ForReading) > objSelection.TypeText "------------------------------------------" & > vbCrLf > objSelection.TypeText "File Name -" & filename & vbCrLf & vbCrLf > objSelection.TypeText "------------------------------------------" & > vbCrLf > Rem Search the text file till the end of file > Do Until objFile.AtEndOfStream > strSearchString = objFile.ReadLine > Rem Pattern matching > Set colMatches = objRegExp.Execute(strSearchString) > If colMatches.Count > 0 Then > For Each strMatch in colMatches > Rem writing the value to Word file > objSelection.TypeText "Match found -" & strSearchString & vbCrLf & vbCrLf > Next > End If > Loop > > ElseIf filetype = "Microsoft Word Document" Then > WScript.Echo "This is a Word file" > > wordPath = "Z:\Script\"&filename > WScript.Echo "Extract Data from " & filename > Set objWord1 = CreateObject("Word.Application") > objWord1.DisplayAlerts = 0 > objWord1.Documents.Open wordPath, false, True > objSelection.TypeText "------------------------------------------" & > vbCrLf > objSelection.TypeText "File Name -" & filename & vbCrLf & vbCrLf > objSelection.TypeText "------------------------------------------" & > vbCrLf > Set objDoc1 = objWord1.ActiveDocument > Set colParagraphs = objDoc1.Paragraphs > For Each objParagraph In colParagraphs > strContents = objParagraph.Range.Text > Set colMatches = objRegExp.Execute(strContents) > For Each strMatch in colMatches > objSelection.TypeText "Match found -" & strContents & vbCrLf & vbCrLf > Next > Next > objDoc1.Close > Set objDoc1 = Nothing > objWord1.Quit > Set objWord1 = Nothing > > Else > objSelection.TypeText "File: " &filename &" is Invalid File Type" > End If > Loop > > End if > Loop > > objDoc.SaveAs("Z:\Script\Scan_results.doc") > objWord.Quit > - Your code uses no indenting - It has little internal structure The result is that it is almost impossible to see where the various loops start and end. To make them visible I did this: - I saved your code to c:\code.txt - I ran this command: type code.txt | findstr /i "do until loop next if next else for each" > code1.txt - I edited code1.txt and left only those lines that were if/do/next/loop statements. - I used introduced proper indentation. You can see the result below. It is immediately obvious that you have some dangling "end if" and "loop" statements at the end. Do Until objFile.AtEndOfStream If filetype = "Text Document" Then Do Until objFile.AtEndOfStream If colMatches.Count > 0 Then For Each strMatch In colMatches Next End If Loop ElseIf filetype = "Microsoft Word Document" Then For Each objParagraph In colParagraphs For Each strMatch In colMatches Next Next Else End If Loop End If Loop If you run into this type of problem with short code like yours, imagine how much worse it would get with a program spanning a few hundred lines! To avoid this type of trap you need to give your code a proper structre, with each module covering no more than one screen full of lines so that you never lose the overview. Here is your code again, this time structured. 'Global statements go here Do Until objFile.AtEndOfStream ProcessFile objFile.ReadLine Loop '-------------------- 'Process one file '-------------------- Sub ProcessFile(sFileName) WScript.Echo sFileName Set objFileType = objFSO.GetFile(sFilename) If objFileType.Type = "Text Document" Then ProcessTextFile(sFileName) ElseIf filetype = "Microsoft Word Document" Then ProcessWordFile(sFileName) Else objSelection.TypeText "File: " &filename &" is Invalid File Type" End If End Sub '------------------ 'Process a text file '------------------ Sub ProcessTextFile(sName) end Sub etc. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Reading file names and Searching for text Thanks for the advise. I am not good at procedures. But will try to put the code as advised by you. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Reading file names and Searching for text As suggested by you, i have modified the code. But some reason its not working nor it is throwing any error. Any thoughts. Rem Creating Regular Expression for pattern match strString = "green|blue" Set objRegExp = CreateObject("VBScript.RegExp") objRegExp.Pattern = strString objRegExp.IgnoreCase = True objRegExp.Global = True Rem Creating Word Application for writing the output Set objWord = CreateObject("Word.Application") objWord.Visible = False Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.Font.Name = "Arial" objSelection.Font.Size = "10" Rem reading for files names from the text file Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("Z:\Script\filenames.txt", ForReading) Rem reading all the filenames from the textfile one by one Do While Not objFile.AtEndOfStream filename = objFile.ReadLine Loop '---------------------------- ----------------------------- 'Process one file '---------------------------- Sub ProcessFile(filename) Set objFileType = objFSO.GetFile(filename) If objFileType.Type = "Text Document" Then ProcessTextFile(filename) ElseIf filetype = "Microsoft Word Document" Then ProcessWordFile(filename) Else objSelection.TypeText "File: " &filename &" is Invalid File Type" End If End Sub '---------------------------- ----------------------------- 'Process a text file '---------------------------- Sub ProcessTextFile(filename) Rem Searching the text files for pattern match Set objFile = objFSO.OpenTextFile(filename, ForReading) objSelection.TypeText "------------------------------------------" & vbCrLf objSelection.TypeText "File Name -" & filename & vbCrLf & vbCrLf objSelection.TypeText "------------------------------------------" & vbCrLf Rem Search the text file till the end of file Do Until objFile.AtEndOfStream strSearchString = objFile.ReadLine Rem Pattern matching Set colMatches = objRegExp.Execute(strSearchString) If colMatches.Count > 0 Then For Each strMatch in colMatches Rem writing the value to Word file objSelection.TypeText "Match found -" & strSearchString & vbCrLf & vbCrLf Next End If Loop End Sub '----------------------------- 'Process a Word file '------------------------------ Sub ProcessWordFile(filename) wordPath = "Z:\Script\"&filename WScript.Echo "Extract Data from " & filename Set objWord1 = CreateObject("Word.Application") objWord1.DisplayAlerts = 0 objWord1.Documents.Open wordPath, false, True objSelection.TypeText "------------------------------------------" & vbCrLf objSelection.TypeText "File Name -" & filename & vbCrLf & vbCrLf objSelection.TypeText "------------------------------------------" & vbCrLf Set objDoc1 = objWord1.ActiveDocument Set colParagraphs = objDoc1.Paragraphs For Each objParagraph In colParagraphs strContents = objParagraph.Range.Text Set colMatches = objRegExp.Execute(strContents) For Each strMatch in colMatches objSelection.TypeText "Match found -" & strContents & vbCrLf & vbCrLf Next Next objDoc1.Close Set objDoc1 = Nothing objWord1.Quit Set objWord1 = Nothing End Sub '------------------------------ objDoc.SaveAs("Z:\Script\Scan_results.doc") objWord.Quit |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Reading file names and Searching for text "Codeblack" <Codeblack@xxxxxx> wrote in message news 60A188E-4C28-4A1C-9E9D-169A00DA8175@xxxxxxQuote: > As suggested by you, i have modified the code. But some reason its not > working nor it is throwing any error. Any thoughts. > > > Rem Creating Regular Expression for pattern match > strString = "green|blue" > Set objRegExp = CreateObject("VBScript.RegExp") > objRegExp.Pattern = strString > objRegExp.IgnoreCase = True > objRegExp.Global = True > > Rem Creating Word Application for writing the output > Set objWord = CreateObject("Word.Application") > objWord.Visible = False > Set objDoc = objWord.Documents.Add() > Set objSelection = objWord.Selection > objSelection.Font.Name = "Arial" > objSelection.Font.Size = "10" > > Rem reading for files names from the text file > Const ForReading = 1 > Set objFSO = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO.OpenTextFile("Z:\Script\filenames.txt", ForReading) > > Rem reading all the filenames from the textfile one by one > Do While Not objFile.AtEndOfStream > filename = objFile.ReadLine > Loop > > '---------------------------- > ----------------------------- > 'Process one file > '---------------------------- > Sub ProcessFile(filename) > Set objFileType = objFSO.GetFile(filename) > If objFileType.Type = "Text Document" Then > ProcessTextFile(filename) > ElseIf filetype = "Microsoft Word Document" Then > ProcessWordFile(filename) > Else > objSelection.TypeText "File: " &filename &" is Invalid File Type" > End If > End Sub > '---------------------------- > ----------------------------- > 'Process a text file > '---------------------------- > Sub ProcessTextFile(filename) > Rem Searching the text files for pattern match > Set objFile = objFSO.OpenTextFile(filename, ForReading) > objSelection.TypeText "------------------------------------------" & > vbCrLf > objSelection.TypeText "File Name -" & filename & vbCrLf & vbCrLf > objSelection.TypeText "------------------------------------------" & > vbCrLf > Rem Search the text file till the end of file > Do Until objFile.AtEndOfStream > strSearchString = objFile.ReadLine > Rem Pattern matching > Set colMatches = objRegExp.Execute(strSearchString) > If colMatches.Count > 0 Then > For Each strMatch in colMatches > Rem writing the value to Word file > objSelection.TypeText "Match found -" & strSearchString & vbCrLf & vbCrLf > Next > End If > Loop > End Sub > '----------------------------- > 'Process a Word file > '------------------------------ > Sub ProcessWordFile(filename) > wordPath = "Z:\Script\"&filename > WScript.Echo "Extract Data from " & filename > Set objWord1 = CreateObject("Word.Application") > objWord1.DisplayAlerts = 0 > objWord1.Documents.Open wordPath, false, True > objSelection.TypeText "------------------------------------------" & > vbCrLf > objSelection.TypeText "File Name -" & filename & vbCrLf & vbCrLf > objSelection.TypeText "------------------------------------------" & > vbCrLf > Set objDoc1 = objWord1.ActiveDocument > Set colParagraphs = objDoc1.Paragraphs > For Each objParagraph In colParagraphs > strContents = objParagraph.Range.Text > Set colMatches = objRegExp.Execute(strContents) > For Each strMatch in colMatches > objSelection.TypeText "Match found -" & strContents & vbCrLf & vbCrLf > Next > Next > objDoc1.Close > Set objDoc1 = Nothing > objWord1.Quit > Set objWord1 = Nothing > End Sub > '------------------------------ > objDoc.SaveAs("Z:\Script\Scan_results.doc") > objWord.Quit > Do While Not objFile.AtEndOfStream filename = objFile.ReadLine Loop It sets the string "filename" to whatever the code reads from your list of file names. That's all - it doesn't do anything else! You probably wanted something like this instead: Do While Not objFile.AtEndOfStream ProcessFile objFile.ReadLine Loop This would invoke the subroutine "ProcessFile" for each file name. By the way, it's not a good idea to use the same variable names in the parent and the child routine. This can cause problems that are difficult to track down. Instead of writing =================== Sub ProcessFile(filename) Set objFileType = objFSO.GetFile(filename) If objFileType.Type = "Text Document" Then ProcessTextFile(filename) ElseIf filetype = "Microsoft Word Document" Then ProcessWordFile(filename) Else objSelection.TypeText "File: " &filename &" is Invalid File Type" End If End Sub '---------------------------- ----------------------------- 'Process a text file '---------------------------- Sub ProcessTextFile(filename) Rem Searching the text files for pattern match ============= you might write ============= Sub ProcessFile(filename) Set objFileType = objFSO.GetFile(filename) If objFileType.Type = "Text Document" Then ProcessTextFile(filename) ElseIf filetype = "Microsoft Word Document" Then ProcessWordFile(filename) Else objSelection.TypeText "File: " &filename &" is Invalid File Type" End If End Sub '---------------------------- ----------------------------- 'Process a text file '---------------------------- Sub ProcessTextFile(sName) **** Note the change here! Rem Searching the text files for pattern match =============== In other words: When you pass a variable to a subroutine, use a different name for this variable in the subroutine. |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Reading file names and Searching for text Did the modification but getting the below error "Microsoft VBScript runtime error: Invalid procedure call or argument". The error is pointing at the line Set objFile = objFSO.OpenTextFile(filename, ForReading) Is it that the argument "filename" which we are passing is wrong? |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Reading file names and Searching for text "Codeblack" <Codeblack@xxxxxx> wrote in message news:3E0AC0F3-4C83-4430-BFEC-4507A01B2A91@xxxxxx Quote: > Did the modification but getting the below error > > "Microsoft VBScript runtime error: Invalid procedure call or argument". > > The error is pointing at the line > > Set objFile = objFSO.OpenTextFile(filename, ForReading) > > Is it that the argument "filename" which we are passing is wrong? code editor that lets you step through the code and monitor your variables) then you need to add your own diagnostics, e.g. like so: wscript.echo "Filename=" & filename & "xxx" Set objFile = objFSO.OpenTextFile(filename, ForReading) You will probably see that the variable "filename" does not have a value assigned to it. |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Reading file names and Searching for text Did some modification as below. Now the script is working but again it is iterating only the first 2 filenames and not through all the filenames. Can you please help in identifying what could be wrong. Do While Not objFile.AtEndOfStream filename = objFile.ReadLine 'Changed his ProcessFile filename 'Changed his Wscript.echo filename Loop |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Reading file names and Searching for text I am totally struck. Can anyone please help me. Thanks |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Reading file names and Searching for text On Mar 3, 1:25 pm, "Pegasus \(MVP\)" <I....@xxxxxx> wrote: Quote: > "Codeblack" <Codebl...@xxxxxx> wrote in message > > news:3E0AC0F3-4C83-4430-BFEC-4507A01B2A91@xxxxxx > Quote: > > Did the modification but getting the below error Quote: > > "Microsoft VBScript runtime error: Invalid procedure call or argument". Quote: > > The error is pointing at the line Quote: > > Set objFile = objFSO.OpenTextFile(filename, ForReading) Quote: > > Is it that the argument "filename" which we are passing is wrong? > If you don't have an IDE (an Interactive Development Environment, i.e. a > code editor that lets you step through the code and monitor your variables) > then you need to add your own diagnostics, e.g. like so: > > wscript.echo "Filename=" & filename & "xxx" > Set objFile = objFSO.OpenTextFile(filename, ForReading) > > You will probably see that the variable "filename" does not have a value > assigned to it. use (Outlook Express, web Interface), some of your posts and some of mine are missing. Here is what I wrote a few hours ago: ================ Sorry, your code fragment is too short to make any useful suggestions. And what do you mean with the comment "Changed his"? "His" what? |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Reading A Text File | VB Script | |||
| Reading text file at a URL | VB Script | |||
| Reading a text file with StreamReader | .NET General | |||
| Refresh on Reading more than one variable from a Text file | VB Script | |||
| Reading text file and charting them via powergadget | PowerShell | |||