简体   繁体   English

Powershell新数组,将一个单词与另一个数组串联

[英]Powershell new array from concatenation of one word with another array

In Powershell, I'm trying to make a list of files to be excluded from various commands that take exclusion parameters, eg Get-ChildItem . 在Powershell中,我试图列出要从带有排除参数的各种命令中排除的文件列表,例如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. 我正在处理的文件属于各个项目,并表示顺序的页码,以便对于给定的项目A,它将具有文件projA.1projA.2等。我希望能够列出其中的一个页码 (不是文件)要排除在外,但是将列表应用于多个项目,所以我想做的是拥有一个排除的页码数组,然后将其合并到项目名称上并作为排除参数传递。 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 "? 在我的代码示例中,是否有一个紧凑的表示法来表示“ sourceExclusions是一个新数组,其长度与$pageExclusions相同,但是每个条目都是文件名projA并具有$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 . 或者,甚至更好的是,是否有一种方法可以将它们都直接传递给exclude参数,其解释方式将导致$sourceExclusions

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 这将排除projA.1projB.1等。如果仍然需要它,则仅返回数学文件projA.*projB.*您可以像这样扩展它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM