我正在尝试使用 VBScript 选择特定文件夹中的所有 csv,然后将它们连接成一个。我正在使用 Win32_Directory 上的 ExecQuery 将所有 csv 添加到集合中,并指定路径和扩展属性。我在文件夹中有五个 csv 来测试。但返回的集合始终为空。
这是我的代码:
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
我也不确定我为 OpenTextFile 指定路径的方式是否有效。但我现在主要关心的是让查询返回我想要的文件。
谢谢你的帮助!