我是否需要使用 WMI 才能在 VBScript 中合并文本文件?

Do I need to use WMI in order to merge text files in VBScript?

在搜索有关在 VBScript 中合并文本文件的提示时,我遇到了这个示例:https://gallery.technet.microsoft.com/scriptcenter/Merge-multiple-txt-files-cbe9625c

Const ForReading = 1 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objOutputFile = objFSO.CreateTextFile("output.txt") 

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2") 

Set FileList = objWMIService.ExecQuery _ 
    ("ASSOCIATORS OF {Win32_Directory.Name='z:\Scripts\Test'} Where " _ 
        & "ResultClass = CIM_DataFile") 

For Each objFile In FileList 
    Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading)  
    strText = objTextFile.ReadAll 
    objTextFile.Close 
    objOutputFile.WriteLine strText 
Next 

objOutputFile.Close

使用说明如下:

You can merge multiple txt files from specific folder to one txt file. It will merge all data to one txt file. You dont need to copy data manually. You can execute this script directly or from command promt. In this script you need to change the folder path from z:\Scripts\Test to your existing path where all txt files are available as well as change the name "output.txt" with your require output file name and path.

尽管我对 VBScript 很陌生(并且 非常非常 很长时间没有编写 VBScript),但我不明白使用此 WMI 服务的意义对于这样一个简单的任务(即处理同一文件夹中的文件)。

仅使用 folder.Files 然后根据需要过滤文件是否足够?

感谢您的帮助。

您完全正确,使用 WMI 完成此任务很愚蠢。

这样做会很好,而且速度也会快很多:

Option Explicit

Dim FSO, OutputFile, File

Set FSO = CreateObject("Scripting.FileSystemObject")
Set OutputFile = FSO.CreateTextFile("output.txt")

For Each File In FSO.GetFolder("Z:\Scripts\Test").Files
    If LCase(FSO.GetExtensionName(File.Name)) = "txt" Then
        With FSO.OpenTextFile(File.Path)
            OutputFile.WriteLine .ReadAll
            .Close
        End With
    End If
Next

OutputFile.Close