我是否需要使用 WMI 来合并 VBScript 中的文本文件? [英] Do I need to use WMI in order to merge text files in VBScript?

查看:132
本文介绍了我是否需要使用 WMI 来合并 VBScript 中的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

说明如下:

您可以将特定文件夹中的多个 txt 文件合并为一个 txt 文件.它将所有数据合并到一个 txt 文件中.您不需要复制数据手动.您可以直接或从命令提示符执行此脚本.在此脚本中,您需要更改 z:\Scripts\Test 中的文件夹路径到您现有的所有 txt 文件都可用的路径以及使用您需要的输出文件名更改名称output.txt"和路径.

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 服务用于如此简单的任务(即处理同一文件夹中的文件).

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).

仅使用 folder.Files 然后根据需要过滤文件还不够吗?

Wouldn't it be enough to just use folder.Files then filter the files to your need?

感谢您的帮助.

推荐答案

你说得对,使用 WMI 来完成这个任务是愚蠢的.

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

这篇关于我是否需要使用 WMI 来合并 VBScript 中的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆