Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace text - if not found carry on

Status
Not open for further replies.

bence8810

IS-IT--Management
Jul 20, 2005
241
AE
Hi,

I have this small VB script I found while googling that replaces commas from a text file. The problem is that I automated the whole process in a batch file and when the text has no commas, the VB script stops with a pop-up message saying that nothing was found, and it wont carry on until I press OK.

Could you help me modify it so that in case if founds nothing, it just quits and lets the batch file carry on forward?

Thanks for yor help,

Ben

Code:
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
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
 
Either [1] host the vbs with cscript.exe (instead of wscript.exe) or [2] comment out the wscript.echo line.
[tt] [highlight]'[/highlight]Wscript.Echo "Searched string Not In the source file"[/tt]
 
Thanks a lot Tsuji, commenting out was easy enough and works like a charm.

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top