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

Type Mismatch in VBScript.... 1

Status
Not open for further replies.

jimsus

Technical User
Sep 25, 2007
5
GB
Hello there!

I'm currently debugging a script I've written at work. Again, it's my fisrt VBscript, so any suggestions are welcome beyond the question I have here! The idea is that anti virus clients publish their logs at a certain time of day, and later on, a script runs from a central server, connects to the remote machines, opens up the logs and pulls out the necessary information (Patch number and engine version)...

The logs are CSV files, and take the following format...

Date,Time,Type,Category,Event,User,Computer,Description

The script looks for the relevent event (4 or 2) and then pulls the needed info out of description and exports it to another CSV file so that can then be mailed on.

The below runs fine if run on the local machine, the oddness comes in when I actually try to connect to remote machines and pull out some info...

I get the error:
scriptname.vbs(79, 3) Microsoft VBScript runtime error: T
ype mismatch: 'arrEngine'

Which is referring to a line that I've highlighted in a comment in the code below.

Odd for me, because the whole thing works like a charm locally on my XP machine, the error happens when running it on a 2003 machine. Any help would be greatly recieved!

Thanks in advance for any suggestions!

Code:
Const ForReading = 1

'Create object that can handle the filesystem
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Output headers to output var
strNewFile = "Server Name, Connection, Pattern, Date Applied, Engine, Date Applied" & vbCrLf

'*************Get Server List**************
'Open Serverlist
Set serverFile = objFSO.OpenTextFile("serverlist.txt")

'Make an array from the server data
RemoteServer = Split(serverFile.ReadAll, vbNewLine)

'Close data file
serverFile.Close

'Iterate through servers
For Each Server In RemoteServer
	
	'Check to see if remote file exists
	If objFSO.FileExists("\\" & Server & "\C$\Program Files\file\location\update.csv") Then
	
		'If it does, open it!
		Set objTextFile = objFSO.OpenTextFile("\\" & Server & "\C$\Program Files\file\location\update.csv", ForReading)
	
		'Iterate through the remote log, one line at a time.
		Do Until objTextFile.AtEndOfStream
		
			'Read a line from the file
			strNextLine = objTextFile.Readline
			
			'Split the columns into an array
			arrFields = Split(strNextLine , ",")
		
			If arrFields(4) = "4" Then
				arrEngine = arrFields 'ERROR HAPPENS HERE
			End If
			
			If arrFields(4) = "2" Then
				arrPattern = arrFields
			End If
		Loop
	
		'Extract engine and pattern number out of the array.
		strEngine = arrEngine(7)
		strLength = len(strEngine)
		engSize = strLength - 54
		strEngine = mid(strEngine, 54, engSize)
		
		strEngDate = arrEngine(0)
		
		strPattern = arrPattern(7)
		strLength = len(strPattern)
		pattSize = strLength - 54
		strPattern = mid(strPattern, 54, pattSize)
		
		strPatDate = arrPattern(0)
		
		strConnection = "Yes"
	Else 'If file does not exist...
		strConnection = "No"
		strPattern = null
		strEngine = null
		strEngDate = null
		strPatDate = null
	End If
	
	'Add formatted line to the file that will be written to CSV file
	strNewFile = strNewFile & Server & "," & strConnection & "," & strPattern & "," & strPatDate & "," & strEngine & "," & strEngDate & vbCrLf
Next

'create output csv file
Set outputFile = objFSO.CreateTextFile("output.csv")

'Write processed info to file
outputFile.Write strNewFile

'Close file
outputFile.Close
 
Are you running 5.1, 5.6 or 5.7 on both machines?

Type cscript and see what version it is. By default, I think 5.1 is installed. You have to upgrade it to 5.6 or 5.7 with a download from MS.
 
Thought it might be something to do with version numbers, but it turns out that both my local XP box and the 2003 server are running 5.6...

Hmmm...
 
It is most probably the coding unicode/system default on the remote file. Do this instead. (3rd parameter false does not matter here as the file exists.)

Set objTextFile = objFSO.OpenTextFile("\\" & Server & "\C$\Program Files\file\location\update.csv", ForReading)
[tt]Set objTextFile = objFSO.OpenTextFile("\\" & Server & "\C$\Program Files\file\location\update.csv", ForReading, false, [red]-2[/red])[/tt]

If it still won't work, change it to -1 to specifically target a unicode csv for that machine.
 
Aaaah, sounds like good stuff... Could be onto something here!

OK. So, what happened? Changing it to -2 did nothing to the error code.

Changing it to -1, (To confirm, makes the line look like:)
Code:
Set objTextFile = objFSO.OpenTextFile("\\" & Server & "\C$\Program Files\Trend\SProtect\update.csv", ForReading, false, -1)

Gave a different error:
Code:
Microsoft VBScript runtime error: Subscript out of range: '[number: 4]'

Which refers to this line:
Code:
If arrFields(4) = "4" Then

So I assume that changing it to -1 means the file isn't being read properly & therefore the array is not being made? Just a guess here!

But yes, still, unfortunatly still errors. Bah! I didn't expect this to happen! Ach. Atleast you good people are here to help me out!

:)

 
I have no doubt in my mind it is primo unicode/ascii issue. Second, when you read line, it is always better to make sure the line is actually not blank (you know people like that kind of separation of line/paragraph). Furthermore, it is to check ubound() after split to prepare for surprise. All that is because the file is not under strict control of those who run the above script-so you can't take it for granted for many things.
 
Mm.. Thanks! Your suggestions are mighty fine. Thank you! Once this little issue is resolved, I'll hop right on making changes in order to make the script more robust.

But for the purposes of testing, I've got access to the data that is being read on the remote server and can confirm it's taking the right format. It's not open to layout change much as it's generated by the AntiVirus program is the same standard every time, but still, I'll make the changes. Never argue with a pro!

Putting aside the other suggestions for the moment (Let's get it all up and running first!) How else could I get around this unicode/ascii issue? The modifications to the line seemed to spawn another error I don't quite understand!

What gets me, is that it runs fine from my lappy! Maybe I should just rack up my laptop in the comms room and be done with it! ;)

 
Sorry ladies and gentlemen, I'm still struggling with this one if there's anybody around who can help... I'm more than happy to provide any additional info!

As a result of tsuji's kind help, I've been looking at unicode / ascii. What I've done is to put up a test csv file, renamed it to .txt in notepad, and saved it at both unicode and ascii. I then set up the appropriate parameter in the OpenTextFile function to force unicode or ascii support.

...But I'm still getting the same errors as outlined above. Do my tests discount the unicode/ascii suggestion? Or am I barking up the wrong tree?

Thanks in advance. I'll post a beer to the winner! ;)
 
Insert before the line getting the error some echo like this.
[tt] wscript.echo len(trim(strNextLine)) & vbcrlf & ubound(arrFields)[/tt]
The ubound() should all the time gives you a number >4 to be right for obvious reason, in particular, it shouldn't be -1. And len() should not be zero, usually.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top