functions/Compress-FileByDate.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
52
53
54

Function Compress-FilesByDate {
    [CmdletBinding()]
    param(
        # Source Directory to archive files from
        [Parameter(Position=0)]
        [String] $Source,

        # Destination to create the Zip in
        [Parameter(Position=1)]
        [String] $Destination,

        # File Suffix
        [Parameter(Position=2)]
        [String] $Suffix,

        # Whether to create the archive by year
        [Switch] $ByYear,

        # Whether to create the archive by month
        [Switch] $ByMonth,

        # Whether to create the archive by day
        [Switch] $ByDay,

        # Whether to remove the file after archiving it
        [Switch] $RemoveAfterArchive
    )
    if(![Boolean](@($ByYear,$ByMonth,$ByDay)|Where-Object{$_})){
        throw "No Archive Flags specified"
    }

    Get-ChildItem $Source -File |
        ForEach-Object {
            $archiveName = (@(
                if($ByYear){$_.LastWriteTime.Year}
                if($ByMonth){$_.LastWriteTime.Month}
                if($ByDay){$_.LastWriteTime.Day}
                $Suffix
            ) | Where-Object {$_}) -join "-"

            $compressParams = @{
                DestinationPath = "$Destination/$archiveName.zip"
                Update = Test-Path "$Destination/$archiveName.zip"
                Path = $_.FullName
            }
            Compress-Archive @compressParams

            if($RemoveAfterArchive){Remove-Item $_.FullName}
        }
}

Export-ModuleMember -Function Compress-FilesByDate