利用vbscript腳本修改文件內容,此適用于自動化的操作中
'新建一個Replace.vbs腳本,腳本內容如下,程序運行時輸入三個參數:查找內容,替換內容,文件
復制代碼 代碼如下:
Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find = WScript.Arguments(0)
ReplaceWith = WScript.Arguments(1)
FileName = WScript.Arguments(2)
'讀取文件
FileContents = GetFile(FileName)
'用“替換內容”替換文件中所有“查找內容”
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
'計算替換總數
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中一個用VBScript寫的隨機數類
- asp,VBscript語法錯誤,史上最全最詳細最精確
- vbscript腳本編程教程2利用fso來進行文件操作
- 使用vbscript腳本在表單中進行選擇的代碼
- 用vbscript腳本實現返回 IP 配置數據的代碼
- ASP里面令人震撼地Debug類(VBScript)
- 調試JavaScript/VBScript腳本程序(IE篇)
- JavaScript/VBScript腳本程序調試(Wscript篇)
- 枚舉域內計算機個數vbscript腳本(沒環境,沒測試)
- ASP/VBScript中CHR(0)的由來以及帶來的安全問題分析
- ASP(VBScript)中整除和取余
- ASP基礎知識VBScript基本元素講解
- ASP基礎入門第四篇(腳本變量、函數、過程和條件語句)