powershell 根据文件名在zip命令中过滤文件

92dk7w1h  于 5个月前  发布在  Shell
关注(0)|答案(1)|浏览(58)

我正在使用powershell压缩文件,并指定只将 *.pdf和 *.xml包含到zip文件中,见下文。

$SourceFiles = Get-ChildItem -Path $SourcePath -Include "*.pdf" , "*.xml"| Select-Object -First $quantity |  Where-Object {$_.lastwritetime -lt (Get-date).AddMinutes($time)}

字符串
我的PDF文件有一个标准的名称,如aaa_bb_cc_123456789.pdf和XML只有123456789.xml。
xml包含与pdf相关的元数据,所以现在我想更清晰的东西,只有zip xml和pdf的文件名中有匹配的字符串(因为完整的文件名将不匹配)。
我不知道如何解决这个问题,所以任何帮助将不胜感激:)

kkbh8khc

kkbh8khc1#

尝试以下方法:

# discover all XML files
$filesToZip = Get-ChildItem -Path $SourcePath -Filter *.xml |ForEach-Object {
  # find corresponding PDF file
  $pdf = Get-Item "*_$([WildcardPattern]::Escape($_.BaseName)).pdf"
  if ($pdf) {
    # we found a valid XML-PDF pair - output both
    $_, $pdf
  }
  else {
    # PDF if missing, warn the caller
    Write-Warning "Found XML file '$($_.Name)' with no corresponding PDF"
  }
}

字符串
$filesToZip现在将包含找到其他文件类型匹配的所有XML和PDF文件的列表

相关问题