Board logo

标题: [转载代码] PowerShell提取zip压缩包中的文件名包含指定字符串的文件 [打印本页]

作者: DAIC    时间: 2013-10-28 17:29     标题: PowerShell提取zip压缩包中的文件名包含指定字符串的文件

  1. ###########################################################
  2. # AUTHOR  : Marius / Hican - http://www.hican.nl - @hicannl
  3. # DATE    : 26-06-2012
  4. # COMMENT : Search zip files for specific files and extract
  5. #           them to a (temp) directory.
  6. ###########################################################
  7. #ERROR REPORTING ALL
  8. Set-StrictMode -Version latest
  9. #----------------------------------------------------------
  10. #STATIC VARIABLES
  11. #----------------------------------------------------------
  12. $search = "xml"
  13. $dest   = "D:\Hican\Temp"
  14. $zips   = "D:\Hican\Test"
  15. #----------------------------------------------------------
  16. #FUNCTION GetZipFileItems
  17. #----------------------------------------------------------
  18. Function GetZipFileItems
  19. {
  20.   Param([string]$zip)
  21.   $split = $split.Split(".")
  22.   $dest = $dest + "\" + $split[0]
  23.   If (!(Test-Path $dest))
  24.   {
  25.     Write-Host "Created folder : $dest"
  26.     $strDest = New-Item $dest -Type Directory
  27.   }
  28.   $shell   = New-Object -Com Shell.Application
  29.   $zipItem = $shell.NameSpace($zip)
  30.   $items   = $zipItem.Items()
  31.   GetZipFileItemsRecursive $items
  32. }
  33. #----------------------------------------------------------
  34. #FUNCTION GetZipFileItemsRecursive
  35. #----------------------------------------------------------
  36. Function GetZipFileItemsRecursive
  37. {
  38.   Param([object]$items)
  39.   ForEach($item In $items)
  40.   {
  41.     If ($item.GetFolder -ne $Null)
  42.     {
  43.       GetZipFileItemsRecursive $item.GetFolder.items()
  44.     }
  45.     $strItem = [string]$item.Name
  46.     If ($strItem -Like "*$search*")
  47.     {
  48.       If ((Test-Path ($dest + "\" + $strItem)) -eq $False)
  49.       {
  50.         Write-Host "Copied file : $strItem from zip-file : $zipFile to destination folder"
  51.         $shell.NameSpace($dest).CopyHere($item)
  52.       }
  53.       Else
  54.       {
  55.         Write-Host "File : $strItem already exists in destination folder"
  56.       }
  57.     }
  58.   }
  59. }
  60. #----------------------------------------------------------
  61. #FUNCTION GetZipFiles
  62. #----------------------------------------------------------
  63. Function GetZipFiles
  64. {
  65.   $zipFiles = Get-ChildItem -Path $zips -Recurse -Filter "*.zip" | % { $_.DirectoryName + "\$_" }
  66.   ForEach ($zipFile In $zipFiles)
  67.   {
  68.     $split = $zipFile.Split("\")[-1]
  69.     Write-Host "Found zip-file : $split"
  70.     GetZipFileItems $zipFile
  71.   }
  72. }
  73. #RUN SCRIPT
  74. GetZipFiles
  75. "SCRIPT FINISHED"
复制代码
http://www.oschina.net/code/snippet_222150_18228
作者: hzcxdgioa    时间: 2013-11-28 22:08

谢谢lz分享。。。。。。。。。▓




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2