利用vbscript腳本修改文件內(nèi)容,此適用于自動(dòng)化的操作中
更新時(shí)間:2006年12月27日 00:00:00 作者:
利用vbscript腳本修改文件內(nèi)容,此適用于自動(dòng)化的操作中
'新建一個(gè)Replace.vbs腳本,腳本內(nèi)容如下,程序運(yùn)行時(shí)輸入三個(gè)參數(shù):查找內(nèi)容,替換內(nèi)容,文件
'新建一個(gè)Replace.vbs腳本,腳本內(nèi)容如下,程序運(yùn)行時(shí)輸入三個(gè)參數(shù):查找內(nèi)容,替換內(nèi)容,文件
復(fù)制代碼 代碼如下:
Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find = WScript.Arguments(0)
ReplaceWith = WScript.Arguments(1)
FileName = WScript.Arguments(2)
'讀取文件
FileContents = GetFile(FileName)
'用“替換內(nèi)容”替換文件中所有“查找內(nèi)容”
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)
'比較源文件和替換后的文件
if dFileContents <> FileContents Then
'保存替換后的文件
WriteFile FileName, dFileContents
Wscript.Echo "Replace done."
If Len(ReplaceWith) <> Len(Find) Then
'計(jì)算替換總數(shù)
Wscript.Echo _
( (Len(dFileContents) - Len(FileContents)) / (Len(ReplaceWith)-Len(Find)) ) & _
" replacements."
End If
Else
Wscript.Echo "Searched string Not In the source file"
End If
'讀取文件
function GetFile(FileName)
If FileName<>"" Then
Dim FS, FileStream
Set FS = CreateObject("Scripting.FileSystemObject")
on error resume Next
Set FileStream = FS.OpenTextFile(FileName)
GetFile = FileStream.ReadAll
End If
End Function
'寫文件
function WriteFile(FileName, Contents)
Dim OutStream, FS
on error resume Next
Set FS = CreateObject("Scripting.FileSystemObject")
Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function
Find = WScript.Arguments(0)
ReplaceWith = WScript.Arguments(1)
FileName = WScript.Arguments(2)
'讀取文件
FileContents = GetFile(FileName)
'用“替換內(nèi)容”替換文件中所有“查找內(nèi)容”
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)
'比較源文件和替換后的文件
if dFileContents <> FileContents Then
'保存替換后的文件
WriteFile FileName, dFileContents
Wscript.Echo "Replace done."
If Len(ReplaceWith) <> Len(Find) Then
'計(jì)算替換總數(shù)
Wscript.Echo _
( (Len(dFileContents) - Len(FileContents)) / (Len(ReplaceWith)-Len(Find)) ) & _
" replacements."
End If
Else
Wscript.Echo "Searched string Not In the source file"
End If
'讀取文件
function GetFile(FileName)
If FileName<>"" Then
Dim FS, FileStream
Set FS = CreateObject("Scripting.FileSystemObject")
on error resume Next
Set FileStream = FS.OpenTextFile(FileName)
GetFile = FileStream.ReadAll
End If
End Function
'寫文件
function WriteFile(FileName, Contents)
Dim OutStream, FS
on error resume Next
Set FS = CreateObject("Scripting.FileSystemObject")
Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function
您可能感興趣的文章:
- ASP、vbscript編碼模板
- ASP中一個(gè)用VBScript寫的隨機(jī)數(shù)類
- asp,VBscript語法錯(cuò)誤,史上最全最詳細(xì)最精確
- vbscript腳本編程教程2利用fso來進(jìn)行文件操作
- 使用vbscript腳本在表單中進(jìn)行選擇的代碼
- 用vbscript腳本實(shí)現(xiàn)返回 IP 配置數(shù)據(jù)的代碼
- ASP里面令人震撼地Debug類(VBScript)
- 調(diào)試JavaScript/VBScript腳本程序(IE篇)
- JavaScript/VBScript腳本程序調(diào)試(Wscript篇)
- 枚舉域內(nèi)計(jì)算機(jī)個(gè)數(shù)vbscript腳本(沒環(huán)境,沒測試)
- ASP/VBScript中CHR(0)的由來以及帶來的安全問題分析
- ASP(VBScript)中整除和取余
- ASP基礎(chǔ)知識(shí)VBScript基本元素講解
- ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過程和條件語句)
相關(guān)文章
VBS 斷網(wǎng)后自動(dòng)關(guān)機(jī)30秒后
只要運(yùn)行它后,一旦網(wǎng)線被拔掉的話,馬上就進(jìn)入自動(dòng)關(guān)機(jī)倒計(jì)時(shí),“-t 30”是倒計(jì)時(shí)的時(shí)間,你可以自己調(diào)整。2010-03-03使用 Iisftpdr.vbs 列出FTP虛擬目錄(支持遠(yuǎn)程與本地)
這篇文章主要介紹了如何通過 iisftpdr.vbs 列出本地或遠(yuǎn)程計(jì)算機(jī)上的 FTP 虛擬目錄的方法,需要的朋友可以參考下2014-07-07VBScript版的PHP extract()函數(shù)
這篇文章主要介紹了VBScript版的PHP extract()函數(shù),extract函數(shù)主要作用是把數(shù)組轉(zhuǎn)換成變量,非常好用的一個(gè)函數(shù),需要的朋友可以參考下2014-06-06