Asp Codes
ActiveX
ADO
Array
Code Snippets
Components
Data Access
Date Time
Files
Graphics
HTML Formatting
Mathematics
Miscellaneous
Sessions
SQL
Strings
Techniques
XML
Asp > Strings sample source codes
Find and Replace
Find and Replace 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
Privacy Policy
|
Link to Us
|
Links