-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompressFolderContents.ps1
51 lines (36 loc) · 1.25 KB
/
CompressFolderContents.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
param(
[string]$SourceDirectory="C:\Home\dev\repo\Surf"
)
##Add required libs
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
function CheckPath([string] $path) {
If ([string]::IsNullOrEmpty($path) -eq $true)
{
Throw "The path must be specified."
}
[bool] $pathExists = Test-Path $path
If ($pathExists -eq $false)
{
Throw "File does not exist (" + $path + ")"
}
}
function CompressSpecificFolder([string]$tocompress,[string] $targetpath){
[System.IO.Compression.ZipFile]::CreateFromDirectory($tocompress, $targetpath)
}
function DeleteFileIfExists([string]$archive){
if (Test-Path $archive) {
Remove-Item $archive
}
}
CheckPath $SourceDirectory
$sourcedirectories = Get-ChildItem $SourceDirectory | Where-Object {$_.Attributes -match'Directory'}
Clear-Host
write-host "########### SPIN THROUGH FILES #############"
foreach ($Path in $sourcedirectories)
{
$targetArchive= [string]::Concat($Path.Parent.FullName,"\",$Path.Name) +"_"+ ((Get-Date).ToString('yyyy-MM-dd')) + ".zip"
Write-Host $targetArchive
Write-Host "Compressing folder " + $Path.FullName
DeleteFileIfExists $targetArchive
CompressSpecificFolder $Path.FullName $targetArchive
}