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?
Set
in VBScript is only for "object-types" (COM Objects, etc), but String
values are not "objects" (as far as VBScript is concerned), so don't use Set
.
Set
keyword but otherwise keep the assignment statement as-is, or altenatively use the Let
keyword instead of Set
: Let
is 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 a Sub
(i.e. a void
function or method) (unless you're using the Call
statement), in this case TextStream.ReadAll()
returns a String
value.
Use the named-constant ForReading
instead of the magic-number literal 1
.
You also should add Option Explicit
at the first-line of your script, otherwise VBScript won't care about variable declarations (indeed: objOutFile
is declared, but objInFile
is what's actually used....)
<job id="Test_Script">
<script language="VBScript">
Option Explicit
Dim objFSO, strSourcePath, strSourceFile, objInFile, strData
Let strSourcePath = "C:\test\ERACSV\"
Let strSourceFile = "Payments.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile(strSourcePath & strSourceFile, ForReading, False)
Let strData = objInFile.ReadAll()
objInFile.Close
WScript.Echo strData
</script>
</job>