#Requires –Version 5.0 # #.Synopsis # Creates a zip file of PowerShell scripts #.Description # The script creates a zip file containing all the files, # recursing through the top level PowerShell Script Library folder. # #.Notes # This script require PowerShell V5 for the zip file cmdlets! # Author - Thomas Lee - tfl@psp.co.uk # #.Example # PS [c:\foo]> .\Zip-PSHScripts3.ps1 # Total files : 347 # ps1 files : 342 # txt files : 1 # other files : 4 # Define what to zip and from where $zipfile = "C:\foo\ScriptLib.ZIP" $zipfrom = "C:\Users\tfl\Dropbox\PowerShell Script Library (master)" $recurse = "true" $ziptoadd = "ps1" # Check it out if ( ! (Test-path -Path $zipfrom )) { Write-Host 'scripts folder does not exist' } # Zip it up! Try { Compress-Archive -Path $zipfrom -DestinationPath $zipfile -CompressionLevel Optimal -Update } Catch { Write-Host ' Error Zipping up the script library' $Error[0] } # Stats $files = ls $zipfrom -file -recurse $files_ps1 = $files | Where-Object Extension -eq '.ps1' $files_txt = $files | Where-Object Extension -eq '.txt' $files_other = $files | Where-Object { $_.extension -NE '.PS1' -and $_.Extension -ne '.txt'} "Total files : {0}" -f $($files.count) "ps1 files : {0}" -f $($files_ps1.count) "txt files : {0}" -f $($files_txt.count) "other files : {0}" -f $($files_other.count) # All done ls $zipfile
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label Zipfile. Show all posts
Showing posts with label Zipfile. Show all posts
Thursday, 27 November 2014
Zip-Pshscripts3.ps1
Monday, 29 July 2013
Get-Zip.ps1
- <#
- .SYNOPSIS
- This script demonstrates the use of the Zip lib in .NET
- .DESCRIPTION
- This script is a re-write of an MSDN sample, using PowerShell
- .NOTES
- File Name : Show-ZIP.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx
- .EXAMPLE
- PSH> .\Show-Zip
- Directory: C:\example
- Mode LastWriteTime Length Name
- ---- ------------- ------ ----
- d---- 7/29/2013 4:35 PM extract
- d---- 7/29/2013 4:29 PM start
- -a--- 7/29/2013 4:35 PM 1668 result.zip
- Directory: C:\example\extract
- Mode LastWriteTime Length Name
- ---- ------------- ------ ----
- -a--- 7/29/2013 4:28 PM 5609 d1.txt
- -a--- 7/29/2013 4:29 PM 67308 d2.txt
- -a--- 7/29/2013 4:29 PM 67308 d3.txt
- -a--- 7/29/2013 4:29 PM 67308 d4.txt
- -a--- 7/29/2013 4:29 PM 67308 d5.txt
- Directory: C:\example\start
- Mode LastWriteTime Length Name
- ---- ------------ ------ ----
- -a--- 7/29/2013 4:28 PM 5609 d1.txt
- -a--- 7/29/2013 4:29 PM 67308 d2.txt
- -a--- 7/29/2013 4:29 PM 67308 d3.txt
- -a--- 7/29/2013 4:29 PM 67308 d4.txt
- -a--- 7/29/2013 4:29 PM 67308 d5.txt
- #>
- # Load the compression namespace
- # And yes, I know this usage is obsolete - but it works.
- # Ignore the output
- [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | out-null
- # Set locations
- $startPath = "c:\example\start"
- $zipPath = "c:\example\result.zip"
- $extractPath = "c:\example\extract"
- Remove-Item $zipPath -ea SilentlyContinue
- Remove-Item -Path $extractPath -inc * -Recurse -ea SilentlyContinue
- # Create the zip file
- [System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath)
- # Extract from zip and show what's all there
- [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath,$extractPath);
- ls c:\example -Recurse
Technorati Tags: System.Io.Compression,ZipFile
Labels:
System.Io.Compression.FileSystem,
Zipfile
Sunday, 24 February 2013
New-ZipFromDirectory.ps1
- <#
- .SYNOPSIS
- Creates a new zip file from an existing folder
- .DESCRIPTION
- This script uses the .NET 4.5 zipfile class
- to create a zip file, getting contents from
- a folder.
- .NOTES
- File Name : New-ZipfromDirectory
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0 and .NET 4.5
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh> C:\foo\new-zip.ps1
- Zip file created:
- Directory: C:\foo
- Mode LastWriteTime Length Name
- ---- ------------- ------ ----
- -a--- 2/24/2013 3:00 PM 291182 ScriptLib.ZIP
- #>
- # Load the compression namespace
- # and yes, I know this usage is obsolete - but it works.
- # Ignore the output
- [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
- # Create a type accellerator for the zipfile class
- [System.Type] $TypeAcceleratorsType=[System.Management.Automation.PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators',$True,$True)
- $TypeAcceleratorsType::Add('Zipfile','System.IO.Compression.Zipfile')
- # Now create a zip file
- # Set target zip flie and source folder
- $Folder = 'E:\PowerShellScriptLib'
- $Zipfile = 'C:\foo\ScriptLib.ZIP'
- # Ensure file does NOT exist and fodler DOES exist
- If (Test-Path $zipfile -EA -0) {
- Throw "$Zipfile exists - not safe to continue"}
- If (!(Test-Path $folder)) {
- Throw "$Folder does not seem to exist"}
- # Now create the Zip file
- Try {
- [Zipfile]::CreateFromDirectory( $folder, $zipfile)
- "Zip file created:";ls $zipfile}
- Catch {
- "Zip File NOT created"
- $Error[0]}
Labels:
.net 4.5,
System.Io.Compression.FileSystem,
zip,
Zipfile
Subscribe to:
Posts (Atom)