使用 powershell 查找并压缩
Find and compress with powershell
有没有办法找到一个文件夹并将其压缩到另一个位置?
这是我的代码
Get-ChildItem $env:USERPROFILE -recurse -ErrorAction SilentlyContinue -Force | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "D877F783D5D3EF8C"}
我不知道如何转换我得到的结果并用它来压缩文件夹
编辑了一点以适合您来自 this 博客的代码:
$source = ( Get-ChildItem $env:USERPROFILE -recurse -ErrorAction SilentlyContinue -Force | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "D877F783D5D3EF8C"} ).FullName
$destination = "Your destination path\zipfilename.zip"
If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination)
$folder = Get-ChildItem $env:USERPROFILE -recurse -ErrorAction SilentlyContinue -Force | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "D877F783D5D3EF8C"} |Select -First 1
if($folder) {
Compress-Archive -LiteralPath $folder.FullName -Destination "C:\path\to\new\archive.zip"
}
else {
Write-Warning "Could not find source directory!"
}
有没有办法找到一个文件夹并将其压缩到另一个位置? 这是我的代码
Get-ChildItem $env:USERPROFILE -recurse -ErrorAction SilentlyContinue -Force | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "D877F783D5D3EF8C"}
我不知道如何转换我得到的结果并用它来压缩文件夹
编辑了一点以适合您来自 this 博客的代码:
$source = ( Get-ChildItem $env:USERPROFILE -recurse -ErrorAction SilentlyContinue -Force | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "D877F783D5D3EF8C"} ).FullName
$destination = "Your destination path\zipfilename.zip"
If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination)
$folder = Get-ChildItem $env:USERPROFILE -recurse -ErrorAction SilentlyContinue -Force | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "D877F783D5D3EF8C"} |Select -First 1
if($folder) {
Compress-Archive -LiteralPath $folder.FullName -Destination "C:\path\to\new\archive.zip"
}
else {
Write-Warning "Could not find source directory!"
}