123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home

Source Codes

Engineering Colleges

BE Students

Training  Reports (updated)

Seminar Reports (updated

Placement Papers (updated)

Forums

   Computer Science / IT

   Electronics

   Electrical

   Mechanical

   Chemical

   Civil

   CAT / MBA

   GMAT / Foreign MBA

Latest Jobs

Engineering Jobs / Technical Jobs

Management Jobs

Sitemap

About-Us

Terms of use

Displaying  Source Code(s)  
 

 
Find and Replace

--------------------------------------------------------------------------------

Description : This will find all occurences of a string in a text file and replace it with another string. Multiple strings can be searched and replaced at the same time.
Parameters: [Filename] [String to search] [String to replace] [Comparison Method (optional)][

Option Explicit
Dim fso
Dim folder
Dim logfile
Dim newfile
Dim deletefile
Dim finalcontents
Dim objArgs
Dim filename
Dim searchstr
Dim replacestr
Dim CompareMethod
Dim Counter
Set objArgs = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
If objArgs.count >= 3 And objArgs.count <=4 Then
filename = objArgs(0)
searchstr = Split(objArgs(1),",")
replacestr = Split(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
' Check the length of search string and
' replace string.
' Both should be equal otherwise error o
' ut
If UBound(searchstr) <> UBound(replacestr) Then
wscript.echo "Search String does Not have corresponding replace string"
wscript.Quit(1)
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
Counter = 0
finalcontents = logfile.readall
Do While Counter <= UBound(searchstr)
If CompareMethod = 0 Then
finalcontents = Replace(finalcontents, Trim(searchstr(counter)), Trim(replacestr(counter)), 1, -1, vbBinaryCompare)
Else
finalcontents = Replace(finalcontents, Trim(searchstr(counter)), Trim(replacestr(counter)), 1, -1, vbTextCompare)
End If
Counter = counter + 1
Loop
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
 

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved copyright 123ENG