2014-01-22 48 views
0

全部,從由Power Shell社區擴展創建的ZIP文件中刪除文件夾

我正在使用適用於PSv1的Power Shell社區擴展並正在創建ZIP文件。但是,我只想要ZIP文件中的圖像,並且我想從ZIP文件中刪除該文件夾。

我有一個名爲newpictures的文件夾被壓縮。然後,我使用Power Shell Community Extensions中的-flattenpaths選項將所有圖像放在基本路徑中,但該文件夾保留。

我一直在尋找解決方案。我不確定是否有這個權利,那麼有人可以查看這段代碼,並在我繼續之前告訴我這是否正確?

if (test-path $PSCXInstallDir) { 
    write-zip -path "Microsoft.PowerShell.Core\FileSystem::$TestSite" -outputpath "Microsoft.PowerShell.Core\FileSystem::$ZipFileCreationDir\$ZipFileName" -noclobber -quiet -flattenpaths -level 9 

    start-sleep -seconds 30 

    if (test-path $ZipFileCreationDir\$ZipFileName) { 
     $ShellApp = new-object -com shell.application 
     $TheZipFile = $ShellApp.namespace("$ZipFileCreationDir\$ZipFileName") 
     $TheZipFile.items() | where-object {$_.name -eq $FolderToCompress} | remove-item $FolderToCompress    
    } 
} 

的變量是:提前任何反饋

$PSCXInstallDir = "C:\Program Files\PowerShell Community Extensions" 
$TestSite = "\\10.0.100.3\www2.varietydistributors.com\catalog\newpictures" 
$ZipFileCreationDir = "\\10.0.100.3\www2.varietydistributors.com\catalog" 
$ZipFileName = "vdi-test.zip" 
$FolderToCompress = "newpictures" 

感謝。總之,我只想刪除ZIP文件中的單個文件夾。

回答

0

Remove-Item不適用於zip文件內的項目。你需要move要壓縮文件外刪除對象之前,你可以將它們刪除:

$ShellApp = New-Object -COM 'Shell.Application' 
$TheZipFile = $ShellApp.NameSpace("$ZipFileCreationDir\$ZipFileName") 
$TheZipFile.Items() | ? { $_.Name -eq $FolderToCompress } | % { 
    $ShellApp.NameSpace($env:TEMP).MoveHere($_) 
    Remove-Item (Join-Path $env:TEMP $_.Name) 
} 

注意Items()不遞歸到嵌套的文件夾,只列舉了文件和當前命名空間的文件夾。如果您需要處理嵌套的文件夾的內容,你需要指定嵌套的路徑:

$NestedFolder = $ShellApp.NameSpace('C:\path\to\your.zip\nested\folder') 

recurse像這樣的東西:

function RecurseIntoZip($fldr) { 
    $fldr.Items() | ? { $_.Name -eq $FolderToCompress } | % { 
    $_.Name 
    } 
    $fldr.Items() | ? { $_.Type -eq 'File folder' } | % { 
    RecurseIntoZip $_.GetFolder 
    } 
} 

RecurseIntoZip $TheZipFile 
+0

所以,當你移動ZIP文件內容的臨時文件夾並刪除文件夾,然後你必須將文件放回ZIP文件或自動完成? ZIP文件中沒有嵌套,因爲Power Shell Community Extensions中用於寫入zip函數的-flattenpaths開關將刪除嵌套。我還試圖弄清楚我是否可以在Power Shell Community Extensions中執行寫入壓縮並完全消除文件夾。我還沒有發現任何東西。 – user3013729

+0

我只是重新讀你的文章,如果我理解正確(小白),你只是移動文件夾以外的ZIP文件,並刪除它,但其他內容留在? – user3013729

+0

@ user3013729正確。 –