Powershell 通过匹配文件名及其父文件夹名称来查找文件并将其添加到 zip 中

问题描述 投票:0回答:1

以下需要 powershell 脚本:

提供逗号分隔的文件名和父文件夹,如下所示:

$itemsToInclude = "stage\arsw.war,dev\deployfile"

我希望在 $workspace="D:\ims" 位置下递归搜索

$itemsToInclude
中提到的所有逗号分隔文件

并且仅将文件名和父文件夹名称都匹配的文件包含到 hello.zip 中

因此

D:\ims\work1\app1\src\stage\arsw.war
应包含在 hello.zip 中,而
D:\ims\work1\app1\src\wow\arsw.war
不应包含在其中,因为父文件夹应为
stage
,如
$itemsToInclude

中所述

因此预期结果是 hello.zip 应包含以下文件。

D:\ims\work1\myapp2\src\dev\deployfile\file1.txt
D:\ims\work1\myapp2\src\dev\deployfile\file2.txt
D:\ims\work1\app1\src\stage\arsw.war

尝试了以下powershell,但失败了:

PS D:\ims> cd D:\ims

$workspace = “D:\ims” 
$itemsToInclude = “stage\arsw.war,dev\deployfile”

$items = $itemsToInclude -split “,” | ForEach-Object { $_.Trim() }

$matchingFiles = @()
foreach ($item in $items) { 
# Get the file name and the parent folder name from the item 

Write-Host "Split-Path -Path $item -Leaf"

$fileName = Split-Path -Path $item -Leaf

Write-Host "Split-Path -Path $item -Parent"

$parentFolder = Split-Path -Path $item -Parent

Write-Host "$fileName and $parentFolder"

$files = Get-ChildItem -Path $workspace -Filter $fileName -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.Directory.Name -eq $parentFolder }


# Add the matching files to the array using array subexpression operator
$matchingFiles = @($matchingFiles; $files)

Write-Host "FOUND $($files.FullName) and $($matchingFiles.FullName)"

if ($matchingFiles) {
Compress-Archive -Path $matchingFiles -DestinationPath hello.zip 
} 
else { 

Write-Host “No matching files were found in $workspace” }

}

输出:

Split-Path -Path stage\arsw.war -Leaf
Split-Path -Path stage\arsw.war -Parent
arsw.war and stage
FOUND D:\ims\work1\app1\src\stage\arsw.war and D:\ims\work1\app1\src\stage\arsw.war
Compress-Archive : The path 'arsw.war' either does not exist or is not a valid file system path.
At line:33 char:1
+ Compress-Archive -Path $matchingFiles -DestinationPath hello.zip
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (arsw.war:String) [Compress-Archive], InvalidOperationException
    + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Compress-Archive
 
Split-Path -Path dev\deployfile -Leaf
Split-Path -Path dev\deployfile -Parent
deployfile and dev
FOUND  and D:\ims\work1\app1\src\stage\arsw.war
Compress-Archive : The path 'arsw.war' either does not exist or is not a valid file system path.
At line:33 char:1
+ Compress-Archive -Path $matchingFiles -DestinationPath hello.zip
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (arsw.war:String) [Compress-Archive], InvalidOperationException
    + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Compress-Archive

此尝试存在一些问题。

  1. dev\deployfile
    内的文件虽然存在但未找到。

  2. dev\deployfile
    下的文件不会添加到hello.zip

请帮忙。

powershell find compression pattern-matching parent
1个回答
0
投票

一种简化的方法是创建一个正则表达式模式来匹配项目的

.FullName
属性。以下仅包含 Zip 存档中的这些文件,但值得注意的是,文件的层次结构在压缩文件中丢失。它们被放置在 Zip 的根目录中。

$itemsToInclude = 'stage\arsw.war,dev\deployfile'
$pattern = $itemsToInclude.Split(',').ForEach({ [regex]::Escape($_) }) -join '|'
$workspace = 'D:\ims'
Get-ChildItem $workspace -Recurse -File |
    Where-Object FullName -Match $pattern |
    Compress-Archive -DestinationPath hello.zip

此外,值得考虑的是,如果

hello.zip
已经存在,您将需要使用
-Update
开关。

© www.soinside.com 2019 - 2024. All rights reserved.