0

In Powershell, I'm trying to make a list of files to be excluded from various commands that take exclusion parameters, e.g. Get-ChildItem.

The files I'm dealing with belong to various projects and represent sequential page numbers such that for a given project A, it would have files projA.1, projA.2, etc. I'd like to be able to make one list of page numbers (not files) to be excluded, but apply that list to multiple projects, so I feel like what I want to do is have an array of excluded page numbers that then get concatendated onto the project name and passed as an exclusion parameter. However, I don't know if there's a compact way to do this. Here's what I have so far:

# Source and destination projects with files named projA.1, projA.2,...,projA.n, etc. 
$sourceProject = "projA"
$destProject = "projB"

# List of pages which will be excluded
$pageExclusions =@( 
"1",
"27",
"28",
"29",
"30",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
"38",
"40")

$sourceExclusions = @()
foreach($i in $pageExclusions){
    $sourceExclusions = $sourceExclusions + ($sourceProject + "." +  $i)
}

# ... later I will use $sourceExclusions as an exclude filter, e.g.:
Get-ChildItem projA.* -Exclude $sourceExclusions

# and I'd need to repeat the same for projB

In my code example, is there a compact notation to say "sourceExclusions is a new array such that is is the same length as $pageExclusions but each entry is the filename projA with the corresponding extension from $pageExclusions"?

Or, even better yet, is there a way to pass them both to an exclude parameter directly in a way it will interpret to result in $sourceExclusions.

1 Answer 1

1

Are you thinking of something like this?

Get-ChildItem -Exclude ($pageExclusions | % { "$sourceProject.$_","$destProject.$_" })

This will exclude projA.1, projB.1 etc. If you still need it only return files mathing projA.* and projB.* you can extend it like this

Get-ChildItem -Exclude ($pageExclusions | % { "$sourceProject.$_","$destProject.$_" }) | ? { $_.Name -match "$sourceProject\.|$destProject\." }
Sign up to request clarification or add additional context in comments.

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.