2

I'm pretty sure Powershell's select-string will work for me with a bit of Get-ChildItem recursion.

I have many directories like this:

abc_123
abc_345
abc_567
abc_890
abc_111
abc_569

In each directory, there's a specific file with the same path to each file in each directory:

abc_123:\AAA\SDK\Settings\foo.settings.xml
abc_345:\AAA\SDK\Settings\foo.settings.xml
abc_567:\AAA\SDK\Settings\foo.settings.xml

And so on.

Within the foo.settings.xml file, I want to search for "BAR." and also one line below it:

  <name="BAR.ABB"
            default="1"

Problem that I'm having is within the directory that has abc_123,abc_345,abc_567, etc, there's other directories that also contain BAR.*. I don't care about those results.

I'm attempting something like this but it's not working.

Get-ChildItem -Recurse *.* | Select-String -Pattern "BAR.*" foo.settings.xml | Select-Object -Unique Path

Where in my command can I specify I only want to search directories beginning with abc_ and at that specific path?

--- edit ---

The path always begins abc_* and foo.settings.xml always has this path: abc_*:\AAA\SDK\Settings\foo.settings.xml

I tried this: Get-ChildItem -Recurse | Where-Object {"abc_*"} | Select-String –Pattern "BAR.*", but this seems to give the same results from above.

1 Answers1

2

In your line Get-ChildItem -Recurse *.* you are specifying that you want all files with any extension, because *.* is binding to the -Filter parameter.

To get all files starting with path abc_* and always with the same relative path you can do something like this: Get-ChildItem -Recurse | Where-Object {$_.FullName -like 'abc_*:\AAA\SDK\Settings\foo.settings.xml'} | Select-String ...

However, given that each foo.settings.xml file is in the same relative directory, this seems relatively inefficient assuming a large number of sub-directories in each abc_* dir. A better solution in this case would be something like:

(where $Path is the root path to the location with all abc_* directories)

Get-ChildItem $Path -Filter abc_* | Foreach-Object {Select-String -Path "$($_.FullName)\AAA\SDK\Settings\foo.settings.xml" -Pattern "BAR.*"}