用于在多个子文件夹中归档文件的powershell脚本

用于在多个子文件夹中归档文件的powershell脚本,powershell,Powershell,我试图写下一个脚本,可以帮助我归档多个子文件夹中的多个文件。我还需要排除特定的文件。 到目前为止,我得到了那几行脚本 $files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg | Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' } foreach ($file in $files) { C:\Program Files\7-zip

我试图写下一个脚本,可以帮助我归档多个子文件夹中的多个文件。我还需要排除特定的文件。 到目前为止,我得到了那几行脚本

$files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg |
  Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' }
foreach ($file in $files)
{
  C:\Program Files\7-zip\7z.exe" a -t7z -mx=9 -ms=on $file
}
基本上,它会递归地搜索.jpeg文件的所有子文件夹,并给出它们的列表,不包括以“i”结尾的文件。jpeg可以说是“photoi.jpeg”。 这是可行的,但我无法进入下一步,因为我需要为所有列出的文件运行7zip

有人能帮帮我吗。
提前感谢:)

不确定您是否正在尝试保存一个大拉链和许多单独的拉链,但是我会这样做:

Set-Alias sz "C:\Program Files\7-zip\7z.exe"

$files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg |
  Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' }
foreach ($file in $files)
{
  $output = sz a -t7z  -mx=9 -ms=on "$File" 2>&1
}

如果我已经使用您的命令行选项对其进行了测试,您可能需要修改压缩线。另一个很好的地方是,我捕获了命令的输出,用于报告目的。

您需要将文件名,而不是对象传递给7z命令。
@列表文件语法可以帮助:

Get-ChildItem ...| Where-Object ...| select -expand fullname| out-file alist.txt -encoding utf8
'C:\Program Files\7-zip\7z.exe' a archive.7z -mx=9 -ms=on `@alist.txt
Remove-item .\alist.txt
注意@