Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Need help with Search and replace VBS  (Read 3134 times)

0 Members and 1 Guest are viewing this topic.

emilware

    Topic Starter


    Starter

    Need help with Search and replace VBS
    « on: May 18, 2009, 08:16:48 PM »
    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 ("<!doctype html public>")
             .writeLn ("<html style=""border-style:outset;" _
             & "border-width:4px"" " _
             & "onKeyDown=""vbscript:SuppressKeys"" " _
             & "onHelp=""vbscript:SuppressIeFns"" " _
             & "onContextMenu=""vbscript:SuppressIeFns"">")
             .writeLn  ("<head>")
             .writeLn   ("<title>" & usTitle & "</title>")
             .writeLn   ("<style type=""text/css"">")
             .writeLn    ("body {background-color:#ece9d8;" _
             & "text-align:center;" _
             & "vertical-align:middle}")
             .writeLn   ("</style>")
             .writeLn   ("<script language=""vbscript"">")
             .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   ("</script>")
             .writeLn  ("</head>")
             .writeLn  ("<body scroll=""no"">")
             .writeLn   ("<table>")
             .writeLn    ("<tr>")
             .writeLn     ("<td style=""text-align:center;" _
             & "font-family:Arial;font-size:16pt;" _
             & "font-weight:bold"">")
             .writeLn      ("Executing Program")
             .writeLn     ("</td>")
             .writeLn    ("</tr>")
             .writeLn    ("<tr>")
             .writeLn     ("</td>")
             .writeLn    ("</tr>")
             .writeLn    ("<tr>")
             .writeLn     ("<td style=""padding-top:15px"">")
             .writeLn      ("<div id=""progresslist"" " _
             & "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       ("<span id=""insertfile""></span>")
             .writeLn      ("</div>")
             .writeLn     ("</td>")
             .writeLn    ("</tr>")
             .writeLn    ("<tr>")
             .writeLn     ("<td style=""padding-top:20px;" _
             & "width:400px;font-family:Arial;" _
             & "font-size:10pt;" _
             & "font-weight:bold"">")
             .writeLn     ("</td>")
             .writeLn    ("</tr>")
             .writeLn   ("</table>")
             .writeLn  ("</body>")
             .writeLn ("</html>")
          End With
          
          
          .visible= True
       End With
       
       WScript.sleep 100
       CreateObject("wscript.shell").appActivate _
       usTitle
       

    emilware

      Topic Starter


      Starter

      Re: Need help with Search and replace VBS
      « Reply #1 on: May 19, 2009, 06:54:25 PM »
      please help

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: Need help with Search and replace VBS
      « Reply #2 on: May 19, 2009, 06:57:21 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.
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      Reno



        Hopeful
      • Thanked: 32
        Re: Need help with Search and replace VBS
        « Reply #3 on: May 20, 2009, 06:10:02 AM »
        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.