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

newbie question

Status
Not open for further replies.

paladin256

Technical User
Apr 16, 2002
189
0
0
US
I have a scanning program that creates a log file with how many viruses found. I need to take the number found and create a new log file that will have a simple text line that is repeated by the number of viruses found. Is there a way to do this? I need to do it this way because the report tool that we use is limited on what it can do and we cannot modify it.

Thank You

 
I have created the following:

Const ForReading = 1

Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set TestFile = fso.CreateTextFile("c:\virusfile.txt", True)

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "Files Infected"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Temp\test.txt", ForReading)

Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
TestFile.WriteLine("Virus Found.")
TestFile.Close ();
Loop
Return 0;
objFile.Close

but I do not know what to do after TestFile.Close statement. Everything I try gives me an error.

The file I am searching write the information in this format
Files Infected: 2

How do I get it so that in the number is zero that it does not write anything but if the number is greater than zero it writes a line in the text file equal to the number found?
 
dont have the TestFile.Close() inside the For Each loop, close it at the end of the script?

you have snuck in some ; line ends and Return 0, which may not be popular.

You are missing an End If to close off you If, a Next to close off your For Each
 
I have changed it as follows:


Const ForReading = 1
Const ForWriting = 2


Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "Files Infected"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Temp\test.txt", ForReading)
Set TestFile = fso.CreateTextFile("c:\temp\virusfile.txt", True)
Set objFile = objFSO.OpenTextFile("C:\Temp\virusfile.txt", ForWriting)
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
TestFile.WriteLine("Virus Found.")
TestFile.Close ()
Next
End If
Loop

objFile.Close

but I now get an error
Line: 12
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
 
Do you have permissions to write to c:\temp?
Are you writing to c:\temp or c:\Temp? do you have both directories present?

Also, I see a potential logic problem: each time you find "Files Infected" you add it to the collection and iterate through the collection. If you find 4 files infected, your log file will have 10 lines that say "Virus Found". Instead of:
Code:
Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString)  
    If colMatches.Count > 0 Then
        For Each strMatch in colMatches   
            TestFile.WriteLine("Virus Found.")
            TestFile.Close ()
        Next
    End If
Loop
You might want:
Code:
Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString)  
Loop
    If colMatches.Count > 0 Then
        For Each strMatch in colMatches   
            TestFile.WriteLine("Virus Found.")
            TestFile.Close ()
        Next
    End If
 
I made the change as suggested but still get the same error. I fixed the typo and do have full permissions to the directory.
 
You are using the variable objFile for both the test.txt and virusfile.txt files.

Set objFile = objFSO.OpenTextFile("C:\Temp\test.txt", ForReading)
Set TestFile = fso.CreateTextFile("c:\temp\virusfile.txt", True)
Set objFile = objFSO.OpenTextFile("C:\Temp\virusfile.txt", ForWriting)
 
How should I be setting it? The examples I have found so far online I thought were doing it that way unless I was reading it wrong. I am not familiar with scripting and am trying to learn this from scratch.
 
Just delete this line entirely:
Code:
Set objFile = objFSO.OpenTextFile("C:\Temp\virusfile.txt", ForWriting)

Or at least comment it out to start with, you should still be able to write to it since you just created it.
 
Thanks for the help. That stopped the error from coming up. If I keep the Loop statement to where you moved it the file is created but is blank. If I put it back where it was the file is created with the text in it but I get error on line 15 char 13 error is vaiable not set code is 800A005B. I think that it is this line if I am reading it right TestFile.WriteLine("Virus Found.") which does not make sense to me since it is inserting the text into the file.


Const ForReading = 1
Const ForWriting = 2
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "Files Infected"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Temp\test.txt", ForReading)
Set TestFile = fso.CreateTextFile("c:\Temp\virusfile.txt", True)
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
TestFile.WriteLine("Virus Found.")
TestFile.Close ()
Next
End If
Loop
objFile.Close
 
Put the "TestFile.Close()" statement at the end of the script (either right before objFile.Close or right after it).
 
As already suggested, put the TestFile.Close statement AFTER the Loop.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top