1.

Solve : Search and Replace files in a folder?

Answer» 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? 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.

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. 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. 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? Quote
Is my pattern wrong?

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

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.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". 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. 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 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. OK. I'll THINK about that. I'll look for some other solutions on this one. Thankz!


Discussion

No Comment Found