Go Back  Xtreme Visual Basic Talk > Other Languages > Web Programming > Need help with looping and files.


Reply
 
Thread Tools Display Modes
  #1  
Old 07-27-2010, 11:50 AM
oddmut oddmut is offline
Newcomer
 
Join Date: Sep 2008
Posts: 18
Default Need help with looping and files.

Hi guys,

I'm trying to get a loop to rework through this code and go through all the files in the folder it's searching through. Currently it only goes through 1 file. I used a
Code:
wscript.echo docName
to let me know if it's going through more files but it isn't. Can't seem to figure it out. Been working on it for two days.

Code:
'=======================================*1*SECTION==============================================
'Sets up Const, Dims, and variables to be used.
'Setting up the constants that will be used later.
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'Setting the variables that will be used.
Dim logName, folderPath, docName, logLine, wrdCnt, searchPhrase

'Sets what each variable will be.
logName = "C:\NewDoc4998-WordSearch1.log"
folderPath = "C:\NewNotepad\PhraseSearchTest\"
searchPhrase = "NewNotepad"

'=======================================*2*SECTION==============================================
'Finds the docName for each file in the folder.
Set objFolder = CreateObject("Scripting.FileSystemObject")
Set objFolderLoc = objFolder.GetFolder(folderPath)

For Each file In objFolderLoc.Files
docName = folderPath + file.Name
wscript.echo docName
Next

'=======================================*3*SECTION==============================================
'Writes the time to the log when it started and inputs file info.
Set objLog = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objLog.OpenTextFile(logName, ForWriting, True)
   
logLine = "GlobalStart: " & Time()
objLogFile.WriteLine(logLine)
objLogFile.WriteLine()

   logLine = "File Name: " & docName 
   objLogFile.WriteLine(logLine)
   logLine = "StartTime: " & Time()
   objLogFile.WriteLine(logLine)
   'objLogFile.Close
   
'=======================================*4*SECTION==============================================
'Opens the file provided. Reads the file and closes it. Searches through the file through memory and returns how
'many patterns match.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(docName)
wscript.echo docname
'Puts file information into memory and then closes file.
strContents = objFile.ReadAll
objFile.Close

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True 'Means not case sensitive when true.
objRegEx.Global = True 'This says keep searching even if there's 1 found when true.
objRegEx.Pattern = searchPhrase

Set colMatches = objRegEx.Execute(strContents)
'Wscript.Echo "Total Matches: " & colMatches.Count

'=======================================*5*SECTION==============================================
'This section runs the loop to keep finding the file with the searchPhrase.
   wrdCnt = colMatches.Count
   Do While True
               If wrdCnt = True Then
                       wrdCnt = wrdCnt + 1
               Else
                       Exit Do
               End If
		Loop

'=======================================*6*SECTION==============================================
'Writes the time to the log when it found and then ended.
'Set objLog = CreateObject("Scripting.FileSystemObject")
'Set objLogFile = objLog.OpenTextFile(logName, ForAppending, False)

'wrdCnt = colMatches.Count

logLine = "Found " & searchPhrase & " " & wrdCnt & " times. EndTime: " & Time()
       objLogFile.WriteLine(logLine)
       objLogFile.WriteLine()
logLine = "GlobalEnd: " & Time()
objLogFile.WriteLine(logLine)
objLogFile.WriteLine()
objLogFile.Close
Reply With Quote
  #2  
Old 07-27-2010, 12:14 PM
00100b's Avatar
00100b 00100b is offline
Martian In Disguise

Retired Moderator
* Guru *
 
Join Date: May 2003
Location: Minneapolis, MN
Posts: 9,572
Default

For clarification. Do you want the execution of sections 3, 4, 5, and 6 to occur for each file in objFolder?

A quick read of the code posted would appear to me as it will only perform those sections on the last assignment to docName, after it exits the For...Each loop.
__________________
protected void Bohica(object somethingBad) {
foreach (Opening o in MyOpeningCollection) { o.Fill(somethingBad); }
}
Reply With Quote
  #3  
Old 07-27-2010, 12:20 PM
oddmut oddmut is offline
Newcomer
 
Join Date: Sep 2008
Posts: 18
Default

yeah that's correct. I tried putting the Do while at the top of 3 but it didn't work.
Reply With Quote
  #4  
Old 07-27-2010, 12:26 PM
00100b's Avatar
00100b 00100b is offline
Martian In Disguise

Retired Moderator
* Guru *
 
Join Date: May 2003
Location: Minneapolis, MN
Posts: 9,572
Default

I've got to be missing something from your description of the issue because I want to say that it is as simple as moving the code for sections 3 through 6 so that it is within the For...Each loop (right before the invocation of the Next statement).
__________________
protected void Bohica(object somethingBad) {
foreach (Opening o in MyOpeningCollection) { o.Fill(somethingBad); }
}
Reply With Quote
  #5  
Old 07-27-2010, 12:51 PM
oddmut oddmut is offline
Newcomer
 
Join Date: Sep 2008
Posts: 18
Default

Okay...I can't believe it was that simple... I thought the whole problem was with the
Code:
 Do While True
               If wrdCnt = True Then
                       wrdCnt = wrdCnt + 1
               Else
                       Exit Do
               End If
		Loop
I guess I have two last questions then. Already tested it and it's echoing the documents for me. So that was successful.

Question 1: I have this output file "C:\NewDoc4998-WordSearch1.log" and it out puts like this
Code:
GlobalStart: 11:47:42 AM

File Name: C:\NewNotepad\PhraseSearchTest\Notepad5.txt
StartTime: 11:47:42 AM
Found NewNotepad 0 times. EndTime: 11:47:43 AM

GlobalEnd: 11:47:43 AM
and every time it goes back to write it overwrites
Code:
File Name: C:\NewNotepad\PhraseSearchTest\Notepad5.txt
StartTime: 11:47:42 AM
Found NewNotepad 0 times. EndTime: 11:47:43 AM
. I've tried closing the file and opening it again. I've tried saving it, which was invalid. I then tried to use the
Code:
Forwriting, True, ForAppending, True
but that also sends back an error. Kinda stumped there.

Last one is since the For Each Loop is handling everything, what's the Do While Loop even doing then? To me it looks like it's doing nothing, because there is no code in the loop.
Reply With Quote
  #6  
Old 07-27-2010, 01:05 PM
00100b's Avatar
00100b 00100b is offline
Martian In Disguise

Retired Moderator
* Guru *
 
Join Date: May 2003
Location: Minneapolis, MN
Posts: 9,572
Default

The opening of the log file and the writing of the start time should really go before the iteration through the files in the For...Each loop and the writing of the end time should really go after all of the files have been closed. So:
Code:
'=======================================*3*SECTION==============================================
'Writes the time to the log when it started and inputs file info.
Set objLog = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objLog.OpenTextFile(logName, ForWriting, True)
   
logLine = "GlobalStart: " & Time()
objLogFile.WriteLine(logLine)
should go before:
Code:
For Each file In objFolderLoc.Files
and:
Code:
logLine = "GlobalEnd: " & Time()
objLogFile.WriteLine(logLine)
objLogFile.WriteLine()
objLogFile.Close
should go after:
Code:
Next
As is, it is reopening the log file for each iteration of the loop and since it is opened ForWriting instead of ForAppending, it will overwrite the file each time.

As far as what the Do While loop is doing, it isn't really doing anything. My assumption is that it is supposed to be counting the number of occurrences of the searchPhrase, but I don't know why you aren't just using the result from colMatches.Count.
__________________
protected void Bohica(object somethingBad) {
foreach (Opening o in MyOpeningCollection) { o.Fill(somethingBad); }
}
Reply With Quote
  #7  
Old 07-27-2010, 01:26 PM
oddmut oddmut is offline
Newcomer
 
Join Date: Sep 2008
Posts: 18
Default

Just want to thank you before you read so you don't miss it. Thanks!

So I learned now that when I initially opened the file to write to I don't have to use ForWriting just because I'm writing to it. I thought I had to have it, that's why I originally had it at the top and put a ForAppending at the bottom. But when it looped the ForWriting created an issue. Learned now that I don't have to have it!

Here's the output now. Successful. I removed 3 entries so it's not so long:
Code:
GlobalStart: 12:18:13 PM

File Name: C:\NewNotepad\PhraseSearchTest\NewMethod.vbs
StartTime: 12:18:13 PM
Found NewNotepad 2 times. EndTime: 12:18:14 PM

File Name: C:\NewNotepad\PhraseSearchTest\Notepad1.txt
StartTime: 12:18:14 PM
Found NewNotepad 0 times. EndTime: 12:18:15 PM

File Name: C:\NewNotepad\PhraseSearchTest\Notepad3.txt
StartTime: 12:18:16 PM
Found NewNotepad 0 times. EndTime: 12:18:16 PM

GlobalEnd: 12:18:19 PM
Regards to the
Code:
colMatches.Count
I was originally only using that to report the count, but I needed to use a loop and so using the Do While loop made it more complicated than just using the For Each loop - which didn't ring a bell to me at the time because I was so focused on the Do While loop.

I'm learning C# and VBScript at the same time. Loops are the hardest things for me right now.
So the final code is:
Code:
'==========================================*1*SECTION================================================
'Sets up Const, Dims, and variables to be used.
'Setting up the constants that will be used later.
Const ForReading = 1
Const ForAppending = 8

'Setting the variables that will be used.
Dim logName, folderPath, docName, logLine, wrdCnt, searchPhrase

'Sets what each variable will be.
logName = "C:\NewDoc4998-WordSearch1.log"
folderPath = "C:\NewNotepad\PhraseSearchTest\"
searchPhrase = "NewNotepad"

'==========================================*2*SECTION================================================
'Finds the docName for each file in the folder which uses a For Each Loop.
Set objFolder = CreateObject("Scripting.FileSystemObject")
Set objFolderLoc = objFolder.GetFolder(folderPath)

'Writes the time to the log when it started and inputs file info.
	Set objLog = CreateObject("Scripting.FileSystemObject")
	Set objLogFile = objLog.OpenTextFile(logName, ForAppending, True)
   
	logLine = "GlobalStart: " & Time()
	objLogFile.WriteLine(logLine)
	objLogFile.WriteLine()

For Each file In objFolderLoc.Files 'Codes in *3*, *4*, *5*, *6*, SECTION are in this For Each loop.
docName = folderPath + file.Name
wscript.echo "Located file path and name which is " + docName

	logLine = "File Name: " & docName 
	objLogFile.WriteLine(logLine)
	logLine = "StartTime: " & Time()
	objLogFile.WriteLine(logLine)
	'objLogFile.Close
   
'==========================================*3*SECTION================================================
'Opens the file provided. Reads the file and closes it. Searches through the file through memory and returns how
'many patterns match.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(docName)
wscript.echo "Reading and searching the file which is " + docName
'Puts file information into memory and then closes file.
strContents = objFile.ReadAll
objFile.Close

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True 'Means not case sensitive when true.
objRegEx.Global = True 'This says keep searching even if there's 1 found when true.
objRegEx.Pattern = searchPhrase

Set colMatches = objRegEx.Execute(strContents)
'Wscript.Echo "Total Matches: " & colMatches.Count

'==========================================*2a*SECTION===============================================
'Writes the time to the log when it found.
wrdCnt = colMatches.Count
logLine = "Found " & searchPhrase & " " & wrdCnt & " times. EndTime: " & Time()
       objLogFile.WriteLine(logLine)
       objLogFile.WriteLine()

Next 'This is for the For Each loop in *2* SECTION

'Writes the time to the log when it ended.
logLine = "GlobalEnd: " & Time()
objLogFile.WriteLine(logLine)
objLogFile.WriteLine()
objLogFile.Close
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb