2012-02-27 41 views
-1

我讀到了解壓ZIP檔案#1導致創建以下腳本:PowerShell的 - 在ZIP壓縮文件修改項目

$shell_app=New-Object -com shell.application 

Get-ChildItem -name *.zip | ForEach-Object { 
$zip_file=$shell_app.NameSpace((Get-Location).path + " \$_") 
$destination=$shell_app.NameSpace((Get-Location).path) 
$destination.copyhere($zip_file.items()) } 

現在我很感興趣,在這個檔案中的項目操作 - 之前例如我解壓所有這些文件,我想添加到他們的文件名,檔案的名稱。 AS我檢查,我可以簡單地輸入

%zip_file.title

得到它(與我想刪除的.zip extenston),但我不知道如何修改文件名。任何人都可以幫忙或提供足夠的資源嗎?

回答

2

我不認爲你可以單獨使用shell對象。我想你將不得不提取文件文件,然後使用PowerShell重命名。這是我的測試代碼。快速和骯髒,但它伎倆。

$zip="c:\work\brain.zip" 
$shell_app=New-Object -com shell.application 

$zip_file=$shell_app.NameSpace($zip) 
$destination=$shell_app.NameSpace("G:\test") 
$zip_file.items() | foreach { 
    $newname="{0}_{1}" -f $zip_file.Title,$_.name 
    Write-Host "Extracting $newname" -ForegroundColor Green 
    $destination.copyhere($_) 
    $oldfile=Join-path $destination.Self.Path -ChildPath $_.name 
    Rename-Item -Path $oldfile -NewName $newname -passthru 

} 

爲什麼不爲每個目標文件創建一個子文件夾?