1.

Solve : Need help with Search and replace VBS?

Answer»

Hi
I'm TRYING to use this script to search and replace strings in src and dat file. The base name is the same for both FILES but extentions are src and dat. I want to be able to select just src file but do sreach and replace on both with out having to run it first on src and the again on dat.
Thanks for your help

heres what i got so far

Dim MyVar

'******opening message
MyVar = MsgBox ("Hello User! 166 Weld renumbering is active! Would you like to proceed?", 3, "Message from EmilWare")
If myvar = 6 Then

Set objDialog = CreateObject("UserAccounts.CommonDialog")

objDialog.Filter = "src/dat files|*.*"
objDialog.InitialDir = "C:\transfer\oldfiles\test4"
intResult = objDialog.ShowOpen
If intResult = 0 Then
Wscript.Quit
End If

sTitle= "Emilware"

nScrW= CreateObject( _
"htmlfile").parentWindow.screen.availWidth
nScrHt= CreateObject( _
"htmlfile").parentWindow.screen.availHeight

showBar oIe, sTitle
WScript.sleep 50

oIe.document.parentWindow.document.scri pt.listop "
"

sInsert = "Program started, please wait...
"
oIe.document.parentWindow.document.scri pt.listop sInsert
WScript.sleep 1000


sInsert = "Flie correction in progress, please wait...
"
oIe.document.parentWindow.document.scri pt.listop sInsert


'******file correction**************
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objFile = objFSO.OpenTextFile(objDialog.FileName, ForReading)
strOldText = objFile.ReadAll
objFile.Close


strNewText = Replace(strOldText, "PGNO1=0062", "PGNO1=6200", 1, -1 ,1)
strNewText = Replace(strNewText, "PGNO1=0162", "PGNO1=6201", 1, -1, 1)

strNewText = Replace(strNewText, "sw164w0062", "SW164W6200", 1, -1, 1)
strNewText = Replace(strNewText, "Sw164w0062", "SW164W6200", 1, -1, 1)

Set objFile = objFSO.OpenTextFile(objDialog.FileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

'********* finish message***********
sInsert = "Operation Complete
"
oIe.document.parentWindow.document.scri pt.listop sInsert
WScript.sleep 1000
oIe.quit

MsgBox"Process Complete. Have a GOOD DAY", 0, "Message from EmilWare"

Else

'********* cancel message ***********
MsgBox"Process Canceld !!! No changes made! GOOD BYE.", 0, "Message from EmilWare"

End If

'*************end of APPLICATION **********************************************************************************************************

'**********sub functions
Function showBar (ROIE, usTitle)

Set roIe= CreateObject( _
"internetExplorer.application")

roIe.navigate("about:blank")

Do
WScript.sleep 50
Loop Until roIe.readyState=4

With roIe
.fullScreen= True
.toolbar = False
.statusBar = False
.addressBar = False
.resizable= False
.width= 540
.height= 240
.Left= (nScrW -520) \2
.top= (nScrHt -240) \2

With .document
.writeLn (" ")
.writeLn ("& "border-width:4px"" " _
& "onKeyDown=""vbscript:SuppressKeys"" " _
& "onHelp=""vbscript:SuppressIeFns"" " _
& "onContextMenu=""vbscript:SuppressIeFns"">")
.writeLn ("")
.writeLn ("" & usTitle & "")
.writeLn ("")
.writeLn ("body {background-color:#ece9d8;" _
& "text-align:center;" _
& "vertical-align:middle}")
.writeLn ("")
.writeLn ("")
.writeLn ("function SuppressKeys ()")
.writeLn ("select case window.event.keyCode")
.writeLn ("case 112, 114, 116")
.writeLn ("case else: if NOT " _
& "cbool(window.event.ctrlKey) then " _
& "exit function")
.writeLn ("end select")
.writeLn ("window.event.keyCode= 0")
.writeLn ("window.event.cancelBubble= true")
.writeLn ("window.event.returnValue= false")
.writeLn ("end function")
.writeLn ("function SuppressIeFns ()")
.writeLn ("window.event.cancelBubble= true")
.writeLn ("window.event.returnValue= false")
.writeLn ("end function")
.writeLn ("function ListOp (usInsert)")
.writeln ("window.insertfile.insertAdjacentHtml " _
& """beforeBegin"", usInsert")
.writeln ("window.insertfile.scrollIntoView")
.writeLn ("end function")
.writeLn ("")
.writeLn ("")
.writeLn ("")
.writeLn ("")

& "font-family:Arial;font-size:16pt;" _
& "font-weight:bold"">")
.writeLn ("Executing Program")
.writeLn ("
")
.writeLn ("& "style=""height:150px;width:460px;" _
& "max-height:100%;max-width:100%;" _
& "padding-left:10px;text-align:left;" _
& "font-family:Arial;font-size:10pt;" _
& "font-weight:bold;border-style:inset;" _
& "border-width:thin;overflow:scroll"">")
.writeLn ("")
.writeLn ("")
.writeLn ("
& "width:400px;font-family:Arial;" _
& "font-size:10pt;" _
& "font-weight:bold"">")
.writeLn ("
")
.writeLn ("")
.writeLn ("")
End With


.visible= True
End With

WScript.sleep 100
CreateObject("wscript.shell").appActivate _
usTitle

please helpQuote from: emilware on May 19, 2009, 06:54:25 PM
please help
Please don't double post, this thread is still pretty high up and someone who knows VBS more than me will help you.judging from your script above, isn't something like this should be simple:

Code: [Select]f=objdialog.filename
datfilename=fso.getparentfoldername(f) & "\" & fso.getbasename(f) & ".dat"
wsh.echo datfilename
use fso to open src and dat file and then replace on each file. your script already contain sample on opening file, replacing text and rewrite the file with new text.


Discussion

No Comment Found