Do you like this article? Please, rate it and write review!
Rated:
by Aspin.com users
| |
| | Top messages |
| 4.5.2002 9:16:43 | |
| 22.3.2003 17:07:03 | |
| 22.3.2003 19:18:41 | |
Replace one string to another in a text file | Areas>Languages>VBScript Areas>ASP / ASP.Net>Functions>Conversion Areas>WSH | |
| VBS has a great function - replace. Next VBS script lets you replace string in a text file. Replace works with text compare (case insensitive). Use:
[cscript|wscript] replace.vbs Find Replacewith File
Find … Required. Substring being searched for.
Replacewith … Required. Replacement substring.
File … Source and destination file for the replacement
Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find = WScript.Arguments(0)
ReplaceWith = WScript.Arguments(1)
FileName = WScript.Arguments(2)
'Read source text file
FileContents = GetFile(FileName)
'replace all string In the source file
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)
'Compare source And result
if dFileContents <> FileContents Then
'write result If different
WriteFile FileName, dFileContents
Wscript.Echo "Replace done."
If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
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
'Read text file
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
'Write string As a text file.
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
| |
See also for 'Replace one string to another in a text file' article: |
If you like this page, please include next link on your pages:
<A
Href="http://www.motobit.com/tips/detpg_replfile/"
Title="This script let's you replace string
in a text file using
VBS and FSO."
>Replace one string to another in a text file</A>
|
|