Misc Slideshow

  1. function SearchZIPfiles { 
  2. <# 
  3. .SYNOPSIS  
  4. Search for (filename) strings inside compressed ZIP or RAR files (V2.8). 
  5. .DESCRIPTION 
  6. In any directory containing a large number of ZIP/RAR compressed Web Page files  
  7. this procedure will search each individual file name for simple text strings,  
  8. listing both the source RAR/ZIP file and the individual file name containing 
  9. the string. The relevant RAR/ZIP can then be subsequently opened in the usual 
  10. way. 
  11. Using the '-Table' (or alias '-GridView') switch will show the results with the  
  12. Out-GridView display. 
  13. .EXAMPLE 
  14. extract -find 'library' -path d:\scripts 
  15.  
  16. Searching for 'library' - please wait... (Use CTRL+C to quit) 
  17. [Editor.zip] Windows 7 Library Procedures.mht 
  18. [Editor.rar] My Library collection from 1974 Idi Amin.html 
  19. [Test2.rar] Playlists from library - Windows 7 Forums.mht 
  20. [Test3.rar] Module library functions UserGroup.pdf 
  21. Folder 'D:\Scripts' contains 4 matches for 'library' in 4 file(s)
  22. .EXAMPLE 
  23. extract -find 'backup' -path doc 
  24.  
  25. Searching for 'backup' - please wait... (Use CTRL+C to quit) 
  26. [Web.zip] How To Backup User and System Files.mht 
  27. [Pages.rar] Create an Image Backup.pdf 
  28. Folder 'C:\Users\Sam\Documents' contains 2 matches for 'backup' in 2 file(s)
  29. .EXAMPLE 
  30. extract pdf desk 
  31.  
  32. Searching for 'pdf' - please wait... (Use CTRL+C to quit) 
  33. [Test1.rar] VirtualBox_ Guest Additions package.pdf 
  34. [Test2.rar] W8 How to Install Windows 8 in VirtualBox.pdf 
  35. [Test2.rar] W8 Install Windows 8 As a VM with VirtualBox.pdf 
  36. Folder 'C:\Users\Sam\Desktop' contains 3 matches for 'pdf' in 2 file(s)
  37.  
  38. This example uses the 'extract' alias to find all 'pdf' files on the desktop. 
  39. .NOTES 
  40. The first step will find any lines containing the selected pattern (which can 
  41. be anywhere in the line). Each of these lines will then be split into 2  
  42. headings: Source and Filename. 
  43. Note that there may be the odd occasion where a 'non-readable' character in the 
  44. returned string slips through the net!  
  45. .LINK 
  46. Web Address Http://www.SeaStarDevelopment.BraveHost.com 
  47. #> 
  48.    [CmdletBinding()] 
  49.    param([string][string][Parameter(Mandatory=$true)]$Find
  50.          [string][ValidateNotNullOrEmpty()]$path = $pwd
  51.          [switch][alias("GRIDVIEW")]$table
  52.  
  53.    Set-StrictMode -Version
  54.    switch -wildcard ($path) { 
  55.       'desk*' {  
  56.          $path = Join-Path $home 'desktop\*' ; break 
  57.       
  58.       'doc*'
  59.          $docs = [environment]::GetFolderPath("mydocuments")   
  60.          $path = Join-Path $docs '*'; break 
  61.       
  62.       default {  
  63.          $xpath = Join-Path $path '*' -ea
  64.          if (!($?) -or !(Test-Path $path)) { 
  65.             Write-Warning "Path '$path' is invalid - resubmit" 
  66.             return 
  67.          
  68.          $path = $xpath 
  69.       
  70.    
  71.     
  72.    Get-ChildItem $path -include '*.rar','*.zip'
  73.       Select-String -SimpleMatch -Pattern $find
  74.          foreach-Object
  75.             -begin
  76.                 [int]$count =
  77.                 $container = @{} 
  78.                 $folder = $path.Replace('*',''
  79.                 $lines = @{} 
  80.                 $regex = '(?s)^(?<zip>.+?\.(?:zip|rar)):(?:\d+):.*(\\|/)(?<file>.*\.(mht|html?|pdf))(.*)$' 
  81.                 Write-Host "Searching for '$find' - please wait... (Use CTRL+C to quit)"  
  82.             } ` 
  83.             -process
  84.                 if ( $_ -match $regex ) { 
  85.                    $container[$matches.zip] +=1      #Record the number in each. 
  86.                    $source = $matches.zip -replace [regex]::Escape($folder
  87.                    $file   = $matches.file 
  88.                    $file = $file -replace '\p{S}|\p{Cc}',' '   #Some 'Dingbats'. 
  89.                    $file = $file -replace '\s+',' '         #Single space words. 
  90.                    if ($table) { 
  91.                       $key = "{0:D4}" -f $count 
  92.                       $lines["$key $source"] = $file       #Create a unique key. 
  93.                    
  94.                    else
  95.                       Write-Host "[$source] $file" 
  96.                    
  97.                    $count++  
  98.                 
  99.             } ` 
  100.             -end {  
  101.                 $total = "in $($container.count) file(s)."  
  102.                 $title = "Folder '$($path.Replace('\*',''))' contains $($count) matches for '$find' $total" 
  103.                 if ($table -and $count -gt 0) {         
  104.                    $lines.GetEnumerator() |  
  105.                       Select-Object @{name = 'Source';expression = {$_.Key.SubString(5)}}, 
  106.                                     @{name = 'Match' ;expression = {$_.Value}} | 
  107.                          Sort-Object Match | 
  108.                             Out-GridView -Title $title 
  109.                 
  110.                 else
  111.                    if ($count -eq 0) { 
  112.                       $title = "Folder '$($path.Replace('\*',''))' contains no matches for '$find'."    
  113.                    
  114.                    Write-Host $title 
  115.                 }  
  116.             
  117. } #End function. 
  118.  
  119. New-Alias extract SearchZIPfiles    -Description 'Find Web files inside ZIP/RAR' 
  120. Export-ModuleMember -Function SearchZIPfiles -Alias Extract 
 
Loading...
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.