[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] 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

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

TOP

返回列表