-1

I am trying to create a script that reads the contents of a file and reacts to certain error messages. So far everything is working as intended; however, I cannot get the 'If' statement to perform any action. It is intended to read the file and if a error message occurs within the text document, it will just completely taskill the Script. Here is a sample of what I have so far:

Dim WshShell, objfso, LogData, oShell 
  
Const ForReading = 1    
Set objfso = CreateObject("Scripting.FileSystemObject")    
Set objPL = objfso.OpenTextFile("C:\-----", ForReading)    
Set oShell = WScript.CreateObject("WScript.Shell")  
  
error = "Invalid"   
LogData = objPL.ReadAll    

Sub CheckTextFile    
   Do Until objPL.AtEndOfStream    
      If InStr(1, LogData, error) > 0 Then   
        objPL.Close   
        oShell.AppActivate "taskkill /F /IM WScript.exe"    
        WScript.Sleep 2000    
      End If    
   Loop   
End Sub

----- Also you can assume that in my code I already have a text document created with output stating that there is errors that say "Invalid". It seems that anything within my If statement is ignored and skipped. No actions are ever performed from the If statements.

11
  • It would be helpful to know what type of object LogData is after the ReadAll because you have not declared/explicitly typed it. It may not be what you assume it is. It would also be helpful to know if you have debugged the results of the ReadAll to confirm that the contents are what you think they are. Commented Mar 28 at 21:56
  • @KenWhite: That looks like an (the) answer to me, not a comment. Commented Mar 29 at 3:26
  • @AJD The logData line you see is the first mention of it in my code. I am not too proficient in this language, so I thought that I was assigning Line 5 to the constant “LogData”. Commented Mar 29 at 6:08
  • @TerrenceBusbyTerrance209 You are assigning it, but the interpreter will assume a type because you have not declared it as a particular type. This means that when you use it later, you are also assuming that it looks like what you thought it would look like. When you use it, you are assuming it is a string. When coding, try to remove as many assumptions as possible. Try RubberduckVBA for some hints about implicit behaviour in VB. Commented Mar 29 at 22:09
  • 1
    @user692942. Understood. Perhaps a simpler way I could have put it is: Hey, there are a few assumptions because you have not declared types (VBScript is non-typed anyway), and we have not seen any debugging that tests those assumptions. Commented Apr 1 at 4:21

1 Answer 1

2

LogData = objPL.ReadAll - You're at the end of the stream after this line. That means that the Do Until objPL.AtEndOfStream block is never entered. Why are you reading the entire stream and then looping that same stream? There's no need for that, because you have the entire contents of the stream in LogData before you start looping. Either just check for the content in LogData once, or read the stream line-by-line instead of reading it all.

You should be able to change your loop to something like this (untested!!):

error = "Invalid"   
StopIt = False

Sub CheckTextFile    
   Do Until StopIt = True
      LogData = objPL.ReadAll    
      If InStr(1, LogData, error) > 0 Then   
        StopIt = True
        objPL.Close   
        oShell.AppActivate "taskkill /F /IM WScript.exe"    
        WScript.Sleep 2000    
      End If    
   Loop   
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

My Log file actively logs data while the script is being run to check for possible error messages. That is why I have it looping. Do you think looping is maybe not the right approach? @KenWhite
Looping is fine. You just can't do it based on AtEndOfStream. My suggestion would be to move the LogData = objPL.ReadAll' inside the Do Until` loop above the If, set a new variable right above the If such as StopIt = False, change your loop to Do Until StopIt = True, and if you find the error string set StopIt = True to break out of the loop. See my edit to my answer.
@KenWhite you never set StopIt = True so the loop will never break. Also using taskkill to stop wscript.exe will not guarantee the script ends it would be better to just set StopIt = True which will exit the loop and end the script.
@user692942: You're right about the first. Corrected my oversight. As to the use of taskkill, I have no idea what the user is trying to do or what else is in the script - StopIt = True just guarantees that the Sub CheckTextFile gets exited.
So what you’re saying is, the question was unclear and probably shouldn’t have been answered in the first place? I agree.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.