打开并保存(覆盖)多个xml文件

时间:2015-05-13 07:45:13

标签: xml batch-file vbscript

我需要一个脚本来打开文件夹中的所有xml文件,并立即保存(OverWrite)一个Xml文件。原因是XML文件的编码,我需要以UTF-8保存,重新保存是我找到的最简单的方法。

我怎样才能做到这一点?

`@if (@this==@isBatch) @then
@echo off

cscript //nologo //e:jscript "%~f0" ^
        /input:"input_file.xml" ^
        /output:"output_file.xml" ^
        /from:"x-ansi" ^
        /to:"utf-8"

exit /b

@end
var adTypeText = 2;
var adSaveCreateOverWrite = 2;

var inputFile = WScript.Arguments.Named.Item('input');
var outputFile = WScript.Arguments.Named.Item('output');

var from = WScript.Arguments.Named.Item('from');
var to = WScript.Arguments.Named.Item('to');

var inputStream  = WScript.CreateObject('adodb.stream');
with (inputStream){
    Type = adTypeText;
    Charset = from;
    Open();
    LoadFromFile( inputFile );
}

var outputStream = WScript.CreateObject('adodb.stream')
with (outputStream){
    Type = adTypeText;
    Charset = to;
    Open();
    WriteText( inputStream.ReadText );
    SaveToFile( outputFile, adSaveCreateOverWrite );
}

inputStream.Close()
outputStream.Close()`

1 个答案:

答案 0 :(得分:0)

你的代码太复杂而且错综复杂。

您的主要问题很简单,因为这可以计算文件夹中文档中的单词显示。

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFolder("c:\")
For Each thing in f.files
    If LCase(Right(thing.path, 3)) = "doc" Then
        Set doc = GetObject(thing.path)
                For each wd in doc.words
                    If wd = "cat" then Counter = Counter + 1
                Next
    Doc.close
    Set Doc = Nothing
    End If
Next
MsgBox Counter

您可以在循环中使用流Charset属性。