I'm writing a wsf that opens a csv, reads the contents and prints them like so:
<job id="Test_Script">
<script language="VBScript">
Dim objFSO, strSourcePath, strSourceFile, objInFile, strData
Set strSourcePath = "C:\test\ERACSV\"
Set strSourceFile = "Payments.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile(strSourcePath & strSourceFile, 1, False)
Set strData = objInFile.ReadAll
objInFile.Close
WScript.Echo strData
</script>
</job>
I keep getting this weird error:
Script: C:\Users\myuser\openCSV.wsf
Line: 4
Char: 4
Error: Object Required '[string: "C:\test\ERACSV"]'
Code: 800A01A8
Source: Microsoft VBScript runtime error
It seems weird because why is the object it requiring a string I've already defined?
Setin VBScript is only for "object-types" (COM Objects, etc), butStringvalues are not "objects" (as far as VBScript is concerned), so don't useSet.Setkeyword but otherwise keep the assignment statement as-is, or altenatively use theLetkeyword instead ofSet:Letis for non-object values (including Strings, but also Integers and Doubles).In VBScript, when calling a
Function(or COM Method) that returns a value you need to specify parentheses, you only omit the parentheses when calling aSub(i.e. avoidfunction or method) (unless you're using theCallstatement), in this caseTextStream.ReadAll()returns aStringvalue.Use the named-constant
ForReadinginstead of the magic-number literal1.You also should add
Option Explicitat the first-line of your script, otherwise VBScript won't care about variable declarations (indeed:objOutFileis declared, butobjInFileis what's actually used....)