Monday, 29 July 2013

Get-Zip.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the Zip lib in .NET 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-ZIP.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-Zip 
  17.      
  18.     Directory: C:\example 
  19. Mode        LastWriteTime  Length Name                                                                                                                            
  20. ----        -------------  ------ ----                                                                                                                            
  21. d---- 7/29/2013   4:35 PM        extract                                                                                                                         
  22. d---- 7/29/2013   4:29 PM        start                                                                                                                           
  23. -a--- 7/29/2013   4:35 PM   1668 result.zip                                                                                                                      
  24.  
  25.  
  26.     Directory: C:\example\extract 
  27. Mode        LastWriteTime  Length Name                                                                                                                            
  28. ----        -------------  ------ ----                                                                                                                            
  29. -a---  7/29/2013  4:28 PM    5609 d1.txt                                                                                                                          
  30. -a---  7/29/2013  4:29 PM   67308 d2.txt                                                                                                                          
  31. -a---  7/29/2013  4:29 PM   67308 d3.txt                                                                                                                          
  32. -a---  7/29/2013  4:29 PM   67308 d4.txt                                                                                                                          
  33. -a---  7/29/2013  4:29 PM   67308 d5.txt                                                                                                                          
  34.  
  35.  
  36.     Directory: C:\example\start 
  37. Mode        LastWriteTime     Length Name                                                                                                                            
  38. ----        ------------     ------ ----                                                                                                                            
  39. -a---  7/29/2013  4:28 PM    5609 d1.txt                                                                                                                          
  40. -a---  7/29/2013  4:29 PM   67308 d2.txt                                                                                                                          
  41. -a---  7/29/2013  4:29 PM   67308 d3.txt                                                                                                                          
  42. -a---  7/29/2013  4:29 PM   67308 d4.txt                                                                                                                          
  43. -a---  7/29/2013  4:29 PM   67308 d5.txt                                                                                                                          
  44. #> 
  45.  
  46. # Load the compression namespace  
  47. # And yes, I know this usage is obsolete - but it works.  
  48. # Ignore the output  
  49. [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | out-null 
  50.  
  51. # Set locations 
  52. $startPath = "c:\example\start" 
  53. $zipPath = "c:\example\result.zip" 
  54. $extractPath = "c:\example\extract" 
  55. Remove-Item $zipPath -ea SilentlyContinue 
  56. Remove-Item -Path $extractPath -inc * -Recurse -ea SilentlyContinue 
  57.  
  58. # Create the zip file 
  59. [System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath
  60.  
  61. # Extract from zip and show what's all there 
  62. [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath,$extractPath); 
  63. ls c:\example -Recurse 
  64.          

Sunday, 28 July 2013

Show-NumberGroupSizes.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the NumberGroupSizes 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-NumberGroupSizes.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbergroupsizes%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-NumberGroupSizes.ps1 
  17.     123,456,789,012,345.00 
  18.     12,3456,7890,123,45.00 
  19.     1234567890,123,45.00 
  20. #> 
  21.  
  22. # Get Number Format 
  23. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  24. $nfi = $nf.NumberFormat 
  25.  
  26. [Int64] $myInt = 123456789012345 
  27. $myInt.ToString( "N", $nfi
  28.  
  29. # Display the same value with different groupings 
  30. [int[]] $MySizes1 = 2,3,4 
  31. [int[]] $MySizes2 = 2,3,0 
  32.  
  33. $nfi.NumberGroupSizes = $mySizes1 
  34. $myInt.ToString( "N",$nfi
  35. $nfi.NumberGroupSizes = $mySizes2 
  36. $myInt.ToString( "N", $nfi )