windows - 从powershell递归归档某种类型的所有文件
问题描述
有没有办法使用 Compress-Archive 脚本,当从路径运行时:
- 归档与通配符过滤器匹配的所有文件(例如 *.doc)
- 归档当前文件夹和所有子文件夹中的此类文件
- 保存相对文件夹结构(不过,使用相对或绝对的选项会很好)
我很难让它同时完成所有这三个。
编辑:
以下过滤器和递归,但不维护文件夹结构
Get-ChildItem -Path ".\" -Filter "*.docx" -Recurse |
Compress-Archive -CompressionLevel Optimal -DestinationPath "$pwd\doc.archive-$(Get-Date -f yyyyMMdd.hhmmss).zip"
此项目不递归:
Compress-Archive -Path "$pwd\*.docx" -CompressionLevel Optimal -DestinationPath "$pwd\doc.archive-$(Get-Date -f yyyyMMdd.hhmmss).zip"
在某些时候,我有一个可以递归但不过滤的命令,但现在无法返回。
解决方案
不幸的是,从 Windows PowerShell v5.1 / PowerShell Core 6.1.0 开始,Compress-Archive
它非常有限:
保留子目录树的唯一方法是将目录路径传递给
Compress-Archive
.不幸的是,这样做没有提供仅选择文件子集的包含/排除机制。
此外,生成的存档将在内部包含为输入目录命名的单个根目录(例如,如果您传递
C:\temp\foo
到Compress-Archive
,生成的存档将包含一个foo
包含输入目录的子树的目录 - 而不是在顶层包含C:\temp\foo
' 的内容)。没有保留绝对路径的选项。
一个麻烦的解决方法是创建目录树的临时副本,其中仅包含感兴趣的文件 (
Copy-Item -Recurse -Filter *.docx . $env:TEMP\tmpDir; Compress-Archive $env:TEMP\tmpDir out.zip
- 请注意,将包含空目录)- 鉴于您仍然总是会以存档中的输入目录命名的单个根目录结束,即使这可能对您不起作用 - 请参阅底部的替代方案。
使用替代品可能会更好:
直接使用 .NET v4.5+
[System.IO.Compression.ZipFile]
和[System.IO.Compression.ZipFileExtensions]
类型。
在 Windows PowerShell 中,与 PowerShell Core (v6+) 不同,您通常使用手动加载相关程序集Add-Type -AssemblyName System.IO.Compression.FileSystem
- 见下文。使用外部程序,例如7-Zip
直接使用 .NET v4.5+[System.IO.Compression.ZipFile]
类解决问题:
笔记:
在 Windows PowerShell 中,与 PowerShell Core不同,您大多数情况下使用
Add-Type -AssemblyName System.IO.Compression.FileSystem
.由于 PowerShell 从 Windows PowerShell v5.1 / PowerShell Core 6.1.0 开始不支持隐式使用扩展方法,因此您还必须显式使用
[System.IO.Compression.ZipFileExtensions]
该类。
# Windows PowerShell: must load assembly System.IO.Compression.FileSystem manually.
Add-Type -AssemblyName System.IO.Compression.FileSystem
# Create the target archive via .NET to provide more control over how files
# are added.
# Make sure that the target file doesn't already exist.
$archive = [System.IO.Compression.ZipFile]::Open(
"$pwd\doc.archive-$(Get-Date -f yyyyMMdd.hhmmss).zip",
'Create'
)
# Get the list of files to archive with their relative paths and
# add them to the target archive one by one.
$useAbsolutePaths = $False # Set this to true to use absolute paths instead.
Get-ChildItem -Recurse -Filter *.docx | ForEach-Object {
# Determine the entry path, i.e., the archive-internal path.
$entryPath = (
($_.FullName -replace ([regex]::Escape($PWD.ProviderPath) + '[/\\]'), ''),
$_.FullName
)[$useAbsolutePaths]
$null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
$archive,
$_.FullName,
$entryPath
)
}
# Close the archive.
$archive.Dispose()
推荐阅读
- mongodb - MongoClient $in 不适用于 int 数组
- web - 我应该收取多少费用来使用 shopify 更新网站
- java - 像 C# 中的 cw 一样自动完成 System.out.println("")
- google-app-maker - APPMAKER - 多对多关系不起作用
- ssms-2012 - SQL Server 版本冲突
- r - 在 ggRGB 上叠加 ggR 栅格
- java - 在 docker 上运行的 https spring 应用程序
- iis-7 - 无法启动应用程序池错误:由于权限不足无法写入配置文件
- javascript - 需要同时运行 50 个 selenium web 驱动程序
- java - 如何在函数中保留现有的换行符