vbscript 스크립트를 이용하여 파일 내용을 수정합니다. 자동화 작업에 적합합니다.

1826 단어
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

좋은 웹페이지 즐겨찾기