找到所有特定文件夹并压缩它们
find all specific folders and zip them
当然,我想找到名称为 'test' 的所有文件夹并将它们压缩到具有不同名称的单个文件夹中。
我设法做了一些代码:
$RootFolder = "E:\"
$var = Get-ChildItem -Path $RootFolder -Recurse |
where {$_.PSIsContainer -and $_.Name -match 'test'}
#this is assembly for zip functionality
Add-Type -Assembly "System.IO.Compression.Filesystem"
foreach ($dir in $var) {
$destination = "E:\zip\test" + $dir.Name + ".zip"
if (Test-Path $destination) {Remove-Item $destination}
[IO.Compression.Zipfile]::CreateFromDirectory($dir.PSPath, $destination)
}
它给我一个错误:
Exception calling "CreateFromDirectory" with "2" argument(s): "The given path's format is not supported."
我想知道,我的 $dir
.
路径的正确传递方式是什么
如果您使用的是 v5,我建议您使用 Commandlet
如果你不想使用命令行开关,你可以使用这个:
$FullName = "Path\FileName"
$Name = CompressedFileName
$ZipFile = "Path\ZipFileName"
$Zip = [System.IO.Compression.ZipFile]::Open($ZipFile,'Update')
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Zip,$FullName,$Name,"optimal")
$Zip.Dispose()
从 Get-ChildItem
返回的 PSPath
属性 以 PSProvider
为前缀。 CreateFromDirectory()
method 接受两个字符串;第一个是 sourceDirectoryName
,您可以使用对象中的 Fullname
。
$RootFolder = "E:\"
$Directories = Get-ChildItem -Path $RootFolder -Recurse | Where-Object {
$_.PSIsContainer -And
$_.BaseName -Match 'test'
}
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
foreach ($Directory in $Directories) {
$Destination = "E:\zip\test$($Directory.name).zip"
If (Test-path $Destination) {
Remove-Item $Destination
}
[IO.Compression.ZipFile]::CreateFromDirectory($Directory.Fullname, $Destination)
}
如果您有这样的文件夹结构:
- Folder1
-- Test
- Folder2
-- Test
- Folder3
-- Test
你可以这样做:
gci -Directory -Recurse -Filter 'test*' | % {
Compress-Archive "$($_.FullName)\**" "$($_.FullName -replace '\|:', '.' ).zip"
}
你将获得:
D..Dropbox.Projects.Whosebug-Posh.ZipFolders.Folder1.Test.zip
D..Dropbox.Projects.Whosebug-Posh.ZipFolders.Folder2.Test.zip
D..Dropbox.Projects.Whosebug-Posh.ZipFolders.Folder3.Test.zip
或者,如果您想保留 zips 中的目录结构:
gci -Directory -Recurse -Filter 'test*' | % {
Compress-Archive $_.FullName "$($_.FullName -replace '\|:', '.' ).zip"
}
当然,我想找到名称为 'test' 的所有文件夹并将它们压缩到具有不同名称的单个文件夹中。
我设法做了一些代码:
$RootFolder = "E:\"
$var = Get-ChildItem -Path $RootFolder -Recurse |
where {$_.PSIsContainer -and $_.Name -match 'test'}
#this is assembly for zip functionality
Add-Type -Assembly "System.IO.Compression.Filesystem"
foreach ($dir in $var) {
$destination = "E:\zip\test" + $dir.Name + ".zip"
if (Test-Path $destination) {Remove-Item $destination}
[IO.Compression.Zipfile]::CreateFromDirectory($dir.PSPath, $destination)
}
它给我一个错误:
Exception calling "CreateFromDirectory" with "2" argument(s): "The given path's format is not supported."
我想知道,我的 $dir
.
如果您使用的是 v5,我建议您使用 Commandlet
如果你不想使用命令行开关,你可以使用这个:
$FullName = "Path\FileName"
$Name = CompressedFileName
$ZipFile = "Path\ZipFileName"
$Zip = [System.IO.Compression.ZipFile]::Open($ZipFile,'Update')
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Zip,$FullName,$Name,"optimal")
$Zip.Dispose()
从 Get-ChildItem
返回的 PSPath
属性 以 PSProvider
为前缀。 CreateFromDirectory()
method 接受两个字符串;第一个是 sourceDirectoryName
,您可以使用对象中的 Fullname
。
$RootFolder = "E:\"
$Directories = Get-ChildItem -Path $RootFolder -Recurse | Where-Object {
$_.PSIsContainer -And
$_.BaseName -Match 'test'
}
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
foreach ($Directory in $Directories) {
$Destination = "E:\zip\test$($Directory.name).zip"
If (Test-path $Destination) {
Remove-Item $Destination
}
[IO.Compression.ZipFile]::CreateFromDirectory($Directory.Fullname, $Destination)
}
如果您有这样的文件夹结构:
- Folder1
-- Test
- Folder2
-- Test
- Folder3
-- Test
你可以这样做:
gci -Directory -Recurse -Filter 'test*' | % {
Compress-Archive "$($_.FullName)\**" "$($_.FullName -replace '\|:', '.' ).zip"
}
你将获得:
D..Dropbox.Projects.Whosebug-Posh.ZipFolders.Folder1.Test.zip D..Dropbox.Projects.Whosebug-Posh.ZipFolders.Folder2.Test.zip D..Dropbox.Projects.Whosebug-Posh.ZipFolders.Folder3.Test.zip
或者,如果您想保留 zips 中的目录结构:
gci -Directory -Recurse -Filter 'test*' | % {
Compress-Archive $_.FullName "$($_.FullName -replace '\|:', '.' ).zip"
}