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

Author Topic: Search and Replace files in a folder  (Read 11266 times)

0 Members and 1 Guest are viewing this topic.

dr.b0ts0y

    Topic Starter


    Rookie

    Re: Search and Replace files in a folder
    « Reply #15 on: August 12, 2008, 12:58:02 PM »
    Wow! I mean I want to replace Q10,Q10_,Q10- into Q1. Should this pattern "\s[Qq]1\s" work on that scenario as well?  ???

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Search and Replace files in a folder
    « Reply #16 on: August 12, 2008, 01:19:58 PM »
    I'm having a hard time deciphering your intent.

    If you want to replace Q1 with Q1,Q1_,Q1- then yes, the pattern posted should do it.

    If you want to replace Q1,Q1_,Q1- with Q1, then no, the pattern does not match the data.

    In the script, the input box is the replacement string and the pattern is what you're searching for (hardcoded into RegEx.Pattern).

    Based on your test data, q1 q1 0q1 1q1 q1 0q10 q10 q100 q1, you don't need a regular expression to see that Q1,Q1_,Q1- is an incorrect pattern.

    Did you use the tool I suggested. It won't do the replacement, but it will indicate if the pattern matches the data. Always a good first step.  ;D

    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    dr.b0ts0y

      Topic Starter


      Rookie

      Re: Search and Replace files in a folder
      « Reply #17 on: August 13, 2008, 09:40:29 AM »
      Hey! How can we modify your latest vbscript to have a counter for those affected files only. 'Coz what I want is to show the number of files affected (texts changed) and what are those files thru a msgbox after the process is done.  ;D

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Search and Replace files in a folder
      « Reply #18 on: August 13, 2008, 11:53:50 AM »
      You can use the test method of the regular expression. Not sure what version of the script we're using so I used the last script posted. The new code has not been tested, you make have to tweak it a bit.

      Code: [Select]
      Const ForReading = 1
      Const ForWriting = 2

      strReplace = InputBox("Enter Replacement String: ","Text to replace!")

      Set RegEx = CreateObject("VBScript.RegExp")
      RegEx.Pattern = "(\bq1\b)"
      RegEx.IgnoreCase = True
      RegEx.Global = True

      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.GetFolder("C:\44444444\builder")    'use your directory
      Set fc = f.Files

      count = 0
      For Each fs In fc
         If fso.GetExtensionName(fs) = "xml" Then
            Set fi = fso.OpenTextFile(fs.Path, ForReading)
            strContent = fi.ReadAll
            fi.Close
            Set fi = fso.OpenTextFile(fs.Path, ForWriting)
            retVal = RegEx.Test(strContent)
            If retVal Then
            newContent = RegEx.Replace(strContent, strReplace)
              fi.Writeline newContent
            count = count + 1
            Else
            fi.WriteLine strContent
            End If
            fi.Close
         End if
      Next

      MsgBox "Done! There were " count " files affected"

      Happy trails.  8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      dr.b0ts0y

        Topic Starter


        Rookie

        Re: Search and Replace files in a folder
        « Reply #19 on: August 13, 2008, 12:29:55 PM »
        Thankz Bro! I'll check this code in a bit. Have a 'lil problem 'bout regex. Ex.: My text fiIe has the contents of "Q1", "Q1_1", "Q1_2", "Q10", "Q10_1" ... and I want to change Q1 only into something like "Q50", "Q50_1", "Q50_2" ... For that I'd used the pattern "q1\b|q1_" 'coz per this site -> "http://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx" => 

        x|y -> Matches either x or y. For example, 'z|food' matches "z" or "food". '(z|f)ood' matches "zood" or "food".

        I'd tried it and it didn't worked for me, only "Q1" are affected. Any idea what's the cause? Is my pattern wrong?  ???

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Search and Replace files in a folder
        « Reply #20 on: August 13, 2008, 02:42:47 PM »
        Quote
        Is my pattern wrong?

        Of course it's wrong, it's always the pattern.  ;D

        Try using \s for the white space instead of \b.

        You can try this: "(q1\s|q1_)"

        It would be helpful to let us see the first 10 or so records from the text file. Stringing it out makes it harder to visualize what the actual data looks like.

        Try to keep the pattern simple.
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        dr.b0ts0y

          Topic Starter


          Rookie

          Re: Search and Replace files in a folder
          « Reply #21 on: August 13, 2008, 02:50:10 PM »
          Hey! It still won't work. For example: This is the content of my text file:

          "q1" "q1_1" "q1_2" 0q1 1q1 q1 0q10 q10 q100 q1
          George Bush
          "q10" "q10_1" "q10_2"

          and I want to replace all occurrences of q1 like in "q1", "q1_1", "q1_2" and change them into "q50", "q50_1", and "q50_2". Also, "Q10" should not be affected neither "q10_1" or "q10_2".  ???

          dr.b0ts0y

            Topic Starter


            Rookie

            Re: Search and Replace files in a folder
            « Reply #22 on: August 13, 2008, 05:22:23 PM »
            hey! Your code works but it replaces also the underscore in "Q1_1" or "Q1_2" and turned them into "Q501", "Q502" assuming I replaced Q1 by Q50.  ::)

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Search and Replace files in a folder
            « Reply #23 on: August 13, 2008, 06:06:09 PM »
            You have all the tools you need to get the script working. I'm thinking that regular expressions may not be a solution after all.

            There does not seem to be any pattern to the data, although I did find Waldo ;D It's one thing to replace all the q1 with Q50, it's another to have leading quotes, leading spaces and leading numeric characters along with trailing quotes, trailing spaces and trailing characters with different rules as to when to make a replacement..

            For example, if you use the double quotes in the pattern, you need to use them in the replacement string. This sounds simple enough until you find newly quoted strings that never had quotes in the original text. If you don't use quotes in the pattern, you run the risk of making replacements that shouldn't be made.

            It might be easier to parse each word and decide whether to make a replacement or not. In this manner you could change the pattern depending on the chunk of data you've parsed.

            Good luck.  8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            dr.b0ts0y

              Topic Starter


              Rookie

              Re: Search and Replace files in a folder
              « Reply #24 on: August 13, 2008, 06:18:07 PM »
              OK. I'll think about that. I'll look for some other solutions on this one.  8) Thankz!