0

I use this to get a list of folders containing .h files. **

$type = "*.h"
$HDIRS = dir .\$type -Recurse |
Select-Object Directory -Unique |
Format-Table -HideTableHeaders

** It gives me a list of folders. Now I want "-I " before every foldername. Common string manipulation doesn't seem to work

1
  • the Format-* cmdlets DO NOT give you a string - nor do they give you standard PoSh objects. instead, they give you the chopped up chunks of your objects wrapped in formatting code. ///// so, DO NOT use them for anything other than FINAL output to the screen or a plain text file. Commented May 25, 2021 at 12:49

2 Answers 2

1

You still have rich objects after the select so to manipulate the strings you have to reference the one property you've selected "Directory"

$type = "*.h"
$HDIRS = Dir .\$type -Recurse |
Select-Object Directory -Unique |
ForEach-Object{
    $_.Directory = "-I" + $_.Directory
    $_
} |
Format-Table -HideTableHeaders

This will result in $HDIRS looking like a list of folder paths like -IC:\temp\something\something...

However, the format output objects are generally not suitable for further consumption. It looks like you're interested in the strings you could simply make this a flat array of strings like:

$type = "*.h"
$HDIRS = Dir .\$type" -Recurse |
Select-Object -ExpandProperty Directory -Unique |
ForEach-Object{ "-I" + $_ } 
Sign up to request clarification or add additional context in comments.

Comments

0

The mantra goes filter left, format right.

Our data:

$type = '.\*.h'
Get-ChildItem -Path $type -Recurse

The manipulation (filter):

Select-Object { "-I $($_.Directory)" } -Unique

And the format:

Format-Table -HideTableHeaders

In many cases, PowerShell cmdlets allow you to pass scriptblocks (closures) to evaluate to values and that's what I'm doing above with the Select-Object call.

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.