西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)編程開(kāi)發(fā)其它知識(shí) → WSH實(shí)現(xiàn)批量替換多個(gè)文件中的指定字符串

WSH實(shí)現(xiàn)批量替換多個(gè)文件中的指定字符串

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站整理時(shí)間:2011/1/13 11:03:42字體大。A-A+

作者:佚名點(diǎn)擊:93次評(píng)論:3次標(biāo)簽: 批量替換 WSH

  • 類型:磁盤工具大小:18.8M語(yǔ)言:中文 評(píng)分:5.2
  • 標(biāo)簽:
立即下載

 看到有人用Dos命令寫,看了看For指令頭暈。于是用WSH腳本寫一個(gè),忙活完了發(fā)現(xiàn)不支持UTF-8。
WSH的FSO只支持ANSI,UTF-16,ASCII三種編碼...

下面是代碼了,保存為.wsf后綴雙擊運(yùn)行。extensionReg是文件后綴的正則表達(dá)式,replacePair是替換規(guī)則。
replacePair可以包含多個(gè)替換,每個(gè)替換是一個(gè)數(shù)組元素,也是一個(gè)對(duì)象,此對(duì)象reg屬性是匹配規(guī)則,str屬性是要替換的字條串。

<job id="main">
<script language="JScript">
// Note: This script will not function correctly with non-ANSI Encoding files.
// ################################ Settings ####################### /
// File's extension
var extensionReg = /^.+\.(xml|txt)$/ig;
// Replace rules
// The replacePair Array contains multiple replacement rule objects.
// Each object contains reg and str property.
// The reg property is a Regular Expression
// and the str property replaces every successful match of reg.
var replacePair = [{reg:/\<VALUE\>[^<>]*\<\/value\>/ig, str:"###value###"},
{reg:/\<LABEL\>[^<>]*\<\/label\>/ig, str:"###label###"}];
// ####################################################################### /
// Constant
var WshShell = WScript.CreateObject("WScript.Shell");
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var ANSI = -2, UTF-16 = -1, ASCII = 0;
var fso = new ActiveXObject("Scripting.FileSystemObject");

// Current Folder
var curFolder = fso.GetFolder(WshShell.CurrentDirectory);
// iterator of Files in the current folder
var fc = new Enumerator(curFolder.files);
var counter = 0;
// loop though files
for (; !fc.atEnd(); fc.moveNext())
{
var file = fc.item();
var filefileName = file.Name;
// test of file name match
if (fileName.search(extensionReg) == -1)
continue;
var content = readAllFromFile(file.Path);
var contentcontentRet = content;
var pc = new Enumerator(replacePair);
for (; !pc.atEnd(); pc.moveNext()) {
var replaceItem = pc.item();
contentRetcontentRet = contentRet.replace(replaceItem.reg, replaceItem.str);
}
if (contentRet != content) {
writeStrToFile(file.Path, contentRet);
++counter;
}
}
// Show the result with OK button and "Information Mark" icon.
WshShell.Popup(counter + " files has been replaced.", 0, "Result", 0 + 64);

function readAllFromFile(path) {
var ret = "";
var ts = fso.OpenTextFile(path, ForReading, false, ANSI);
if (!ts.AtEndOfStream)
ret = ts.ReadAll();
ts.Close();
return ret;
}

function writeStrToFile(path, str) {
var ts = fso.OpenTextFile(path, ForWriting, true, ANSI);
ts.Write(str);
ts.Close();
}

// Debug
function trace(msg) {
WScript.Echo(msg);
WScript.Quit();
}
</script>
</job>

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(3)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)