merge - Do I need to use WMI in order to merge text files in VBScript?
问题描述
while searching for hints about merging text files in VBScript, I came accross this example: 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
Instructions are just as follow:
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.
Although I'm quite new at VBScript (and didn't write VBScript for a very very long time), I don't get the point of using this WMI service for such a simple task (i.e. handling files in the same folder).
Wouldn't it be enough to just use folder.Files
then filter the files to your need?
Thanks for your help.
解决方案
You are absolutely right, using WMI for this task is silly.
This will do just fine, and it will be a lot faster, too:
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
推荐阅读
- css - 页眉和页脚中的边框底部长度
- python - numpy 嘈杂的正弦波,具有自定义幅度和频率
- r - 堆叠具有不同列的 data.frames(重命名变量后)
- c++ - vscode 任务:递归指定 .cpp 文件
- python - 内部带有 range() 的 Python 定义函数无法按预期工作
- .htaccess - 从非 www 重定向到 www 不工作从 .htaccess
- mapbox - 为 Mapbox 集群中的点添加不透明度
- php - Symfony 5.1:无法识别的字段:activation_token
- swift - 尝试将某人用 Swift 3 编写的套接字编程代码更新为 Swift 5.2(Inout 表达式创建一个临时指针......)
- php - PHP重写规则页面,如页面-关于不工作