2014-10-18 52 views
2

我一直在使用功能的PowerShell要解壓的大量文件:解壓在PowerShell中不清除臨時文件

function Expand-ZIPFile($file, $destination) 
    { 
     $shell = new-object -com shell.application 
     $zip = $shell.NameSpace($file) 
     foreach($item in $zip.items()) 
     { 
     $shell.Namespace($destination).copyhere($item) 
     } 
    } 

我稱之爲使用類似:

Expand-ZIPFile -File "$dir\$zip_file" -Destination $work_dir 

我發現這個過程在創建一個臨時文件 C:\ Documents and Settings \ ftpuser \ Local Settings \ Temp

對於這些文件中的每一個我都解壓縮了,過去mont它已經生成了近800GB的臨時文件。

問:是否有一種方法來

  1. 從擺在首位創建一個臨時文件停止解壓過程?
  2. 強制我的腳本在退出之前清理這些文件?
+0

我無法在Windows 7中複製這種行爲,這也觀察到這裏(http://forums.anandtech.com/showthread.php?t=2217442)。不理想,但我想你可以從%temp%目錄清空最近的文件。說..最後10分鐘編輯? – Matt 2014-10-18 03:09:47

+0

我會注意到,創建或解壓縮zip文件的Shell.Application破解不是自動化友好的(沒有狀態檢查,它是異步的等)。看到這裏:[http://social.msdn.microsoft.com/Forums/vstudio/en-US/760ea95b-4c3e-4f48-a0f6-9d728d5580da/](http://social.msdn.microsoft.com/Forums/ vstudio/EN-US/760ea95b-4c3e-4f48-a0f6-9d728d5580da /)。使用命令行解壓縮工具(例如[7-Zip](http://www.7-zip.org))的99%時間更簡單。 – 2014-10-18 16:08:45

回答

0

懶惰的解決方案:

$temp = "C:\Documents and Settings\ftpuser\Local Settings\Temp\" 
rm "$temp$($item.Title)" 
0

我懶惰的解決方案(自動):

$temp_dir = "C:\Documents and Settings\ftpuser\Local Settings\Temp" 
    $temp_listing = Get-ChildItem $temp_dir -include *zip -recurse -force | Select-Object Name 
    if ($temp_listing -ne $null) { 
     foreach ($item in $temp_listing) { 
     $item -match "Name=(.+)}" 
     $item = $Matches[1] 
     Write-Host "Removing $temp_dir\$item" 
     if (Test-Path -Path "$temp_dir\$item" -PathType Container) 
     { 
      Remove-Item "$temp_dir\$item" -recurse -force 
     } 
     } 
    } 

這工作,以便腳本:

  1. 找到隱藏的文件
  2. 不提示
  3. 仍然可以在計劃任務內運行。

感謝