PowerShell压缩 - 归档文件扩展
问题描述:
如何使用PowerShell 5.0 Compress-Archive
cmdlet以递归方式将某个目录中的任何.config文件压缩并在保持目录结构的同时进行压缩。示例:PowerShell压缩 - 归档文件扩展
Directory1
Config1.config
Directory2
Config2.config
目标是一个zip文件,其中还包含上述目录结构和仅包含配置文件。
答
我会建议将文件复制到临时目录并压缩。例如:
$path = "test"
$filter = "*.config"
#To support both absolute and relative paths..
$pathitem = Get-Item -Path $path
#If sourcepath exists
if($pathitem) {
#Get name for tempfolder
$tempdir = Join-Path $env:temp "CompressArchiveTemp"
#Create temp-folder
New-Item -Path $tempdir -ItemType Directory -Force | Out-Null
#Copy files
Copy-Item -Path $pathitem.FullName -Destination $tempdir -Filter $filter -Recurse
#Get items inside "rootfolder" to avoid that the rootfolde "test" is included.
$sources = Get-ChildItem -Path (Join-Path $tempdir $pathitem.Name) | Select-Object -ExpandProperty FullName
#Create zip from tempfolder
Compress-Archive -Path $sources -DestinationPath config-files.zip
#Remove temp-folder
Remove-Item -Path $tempdir -Force -Recurse
}
你是什么意思?找到一个配置文件,压缩它(只有配置文件),并将该zip文件保存在与配置文件相同的位置? –
我添加了一个插图。 –
文件结构是唯一清晰的部分tbh。期望的输出是什么样的?你想要一个包含所有配置文件的zip文件?或每个配置文件一个zip文件? –