VB icon

Find & Replace (ver 1.0)

Email
Submitted on: 1/5/2015 5:50:00 PM
By: Sumit Dhingra (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: VbScript (browser/client side)
Views: 748
 
     This will find all occurences of a string in a text file and replace it with another string.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Find & Replace (ver 1.0)
' Description:This will find all occurences of a string in a text file and replace it with another string.
' By: Sumit Dhingra (from psc cd)
'
' Inputs:Filename
String to search
String to replace
Comparison Method (optional)
'
' Returns:Nothing
'
' Assumes:Usage :
Save the code as a .vbs file.
findreplace.vbs mylog.txt Football Baseball 0
Note : CompareMethod can be either 0 (Binary comparison) or 1 (Text comparison).
'**************************************

Option Explicit
Dim fso
Dim folder
Dim logfile
Dim logLine
Dim newfile
Dim deletefile
Dim filecontents
Dim finalcontents
Dim objArgs
Dim filename
Dim folderpath
Dim searchstr
Dim replacestr
Dim CompareMethod
Set objArgs = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
If objArgs.count >= 3 and objArgs.count <=4 then
	filename 	= objArgs(0)
	searchstr 	= objArgs(1)
	replacestr 	= objArgs(2)
	If objArgs.count = 4 then
		CompareMethod = objArgs(3)
		If CompareMethod <> 0 and CompareMethod <> 1 Then
			Wscript.Echo "CompareMethod can only be 0 or 1"
			Wscript.Quit(1)		'To indicate error.
		End If
	Else
		CompareMethod = 0	' Default to vbBinaryCompare.
	End If		
Else
	wscript.echo "Usage: FindReplace.vbs [arguments..]" + vbcrlf + vbcrlf + "Arguments:" + vbcrlf + "File to be Searched" 		+ vbcrlf + "Searched string" + vbcrlf + "String to replace" + vbcrlf + "[Comparison Method]"
	 wscript.Quit (1)	'To indicate error.
End if 
TextSearch(Filename)
 
Function TextSearch(Filename)
	Set logfile = fso.OpenTextFile(filename)
	If Err.Number <> 0 Then
		Wscript.echo Err.Description
		Wscript.Quit (Err.Number)
	End If
	filecontents = logfile.readall
	If CompareMethod = 0 Then
	 	finalcontents = Replace(filecontents, searchstr, replacestr, 1, -1, vbBinaryCompare) 
	Else
		finalcontents = Replace(filecontents, searchstr, replacestr, 1, -1, vbTextCompare) 
	End If
	logfile.Close
	Set deletefile = fso.getFile(filename)
	deletefile.delete
	set newfile = fso.CreateTextFile(filename, true)
	newfile.write FinalContents
	newfile.close
	If Err.Number <> 0 Then
		Wscript.echo Err.Description
		Wscript.Quit (Err.Number)
	End If
	
	set logfile = nothing
	set deletefile	= nothing
	set newfile = nothing
End Function


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.