分享

Powershell提取zip压缩包中的文件

 阳光灿烂的笑笑 2014-09-12
########################################################### 
# AUTHOR  : Marius / Hican - http://www. - @hicannl  
# DATE    : 26-06-2012  
# COMMENT : Search zip files for specific files and extract
#           them to a (temp) directory.
###########################################################
  
#ERROR REPORTING ALL
Set-StrictMode -Version latest
  
#----------------------------------------------------------
#STATIC VARIABLES
#----------------------------------------------------------
$search = "xml"
$dest   = "D:\Hican\Temp"
$zips   = "D:\Hican\Test"
  
#----------------------------------------------------------
#FUNCTION GetZipFileItems
#----------------------------------------------------------
Function GetZipFileItems
{
  Param([string]$zip)
    
  $split = $split.Split(".")
  $dest = $dest + "\" + $split[0]
  If (!(Test-Path $dest))
  {
    Write-Host "Created folder : $dest"
    $strDest = New-Item $dest -Type Directory
  }
  
  $shell   = New-Object -Com Shell.Application
  $zipItem = $shell.NameSpace($zip)
  $items   = $zipItem.Items()
  GetZipFileItemsRecursive $items
}
  
#----------------------------------------------------------
#FUNCTION GetZipFileItemsRecursive
#----------------------------------------------------------
Function GetZipFileItemsRecursive
{
  Param([object]$items)
  
  ForEach($item In $items)
  {
    If ($item.GetFolder -ne $Null)
    {
      GetZipFileItemsRecursive $item.GetFolder.items()
    }
    $strItem = [string]$item.Name
    If ($strItem -Like "*$search*")
    {
      If ((Test-Path ($dest + "\" + $strItem)) -eq $False)
      {
        Write-Host "Copied file : $strItem from zip-file : $zipFile to destination folder"
        $shell.NameSpace($dest).CopyHere($item)
      }
      Else
      {
        Write-Host "File : $strItem already exists in destination folder"
      }
    }
  }
}
  
#----------------------------------------------------------
#FUNCTION GetZipFiles
#----------------------------------------------------------
Function GetZipFiles
{
  $zipFiles = Get-ChildItem -Path $zips -Recurse -Filter "*.zip" | % { $_.DirectoryName + "\$_" }
    
  ForEach ($zipFile In $zipFiles)
  {
    $split = $zipFile.Split("\")[-1]
    Write-Host "Found zip-file : $split"
    GetZipFileItems $zipFile
  }
}
#RUN SCRIPT 
GetZipFiles
"SCRIPT FINISHED"

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多