0

I have the text of a couple hundred Word documents saved into individual .txt files in a folder. I am having an issue where a MergeField in the Word document wasn't formatted correctly, and now I need to find all the instances in the folder where the incorrect formatting occurs. the incorrect formatting is the string \#,$##,##0.00\* So, I'm trying to use PowerShell as follows:

select-string -path MY_PATH\.*txt -pattern '\#,$##,##0.00\*'
select-string -path MY_PATH\.*txt -pattern "\#`,`$##`,##0.00\*"

But neither of those commands finds any results, even though I'm sure the string exists in at least one file. I feel like the error is occurring because there are special characters in the parameter (specifically $ and ,) that I'm not escaping correctly, but I'm not sure how else to format the pattern. Any suggestions?

2 Answers 2

3

If you are actually looking for \#,$##,##0.00\* then you need to be aware that Select-String uses regex and you have a lot of control characters in there. Your string should be

\\\#,\$\#\#,\#\#0\.00\\\*

Or you can use the static method Escape of regex to do the dirty work for you.

[regex]::Escape("\#,$##,##0.00\*")

To put this all together you would get the following:

select-string -path MY_PATH\.*txt -pattern ([regex]::Escape("\#,$##,##0.00\*"))

Or even simpler would be to use the parameter -SimpleMatch since it does not interpet the string .. just searches as is. More here

select-string -path MY_PATH\.*txt -SimpleMatch "\#,$##,##0.00\*"
Sign up to request clarification or add additional context in comments.

Comments

1

My try, similar to Matts:

select-string -path .\*.txt -pattern '\\#,\$##,##0\.00\\\*'

result:

test.txt:1:\#,$##,##0.00\*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.