I'm trying to use VBScript to select all csv's in a specific folder and then concatenate them into one. I am adding all of the csv's to a collection using ExecQuery on Win32_Directory, specifying the path and the extension properties. I have five csv's in the folder to test. But the collection returned is always null.
Here's my code:
strComputer = "."
Set wshShell = WScript.CreateObject( "WScript.Shell" )
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'Options for CreateTextFile
boolOverwrite = True
boolUnicode = True
'Options for OpenTextFile
constForReading = 1
constForWriting = 2
constForAppending = 8
boolCreate = False
constTristate = -1
strPath = "C:\Users\adam\Documents\Test\Temp.csv"
strDrivePath = "C:\Users\adam\Documents\Test"
'Creates object reference to files in relevant folder.
Set objFSO = CreateObject ("Scripting.FileSystemObject")
'Creates new CSV to write others' contents to.
Set objNew = objFSO.CreateTextFile(strPath,boolOverwrite,boolUnicode)
Set colCSV = objWMIService.ExecQuery _
("SELECT * FROM Win32_Directory WHERE Path = strDrivePath AND Extension = 'csv'")
'Concatenates contents of CSVs
For Each objCSV in colCSV
Set objTemp = objFSO.OpenTextFile(objCSV.Path & objCSV.FileName & objCSV.Extension,constForReading,boolCreate,constTristate)
Set strLine = objTemp.ReadLine
objNew.Write strLine
Next
I am also unsure of whether the way I've specified the path for OpenTextFile is going to work or not. But my main concern right now is getting the query to return the files I want.
Thanks for any help!