Site Loader

Hi everyone,

I needed a function that compresses all my files in a certain directory. So I created one instead of looking for one…

This function is PowerShell 5 only!

<#
.Synopsis
   Compresses files based on days,months or years in the past.
.DESCRIPTION
   Compresses files based on days,months or years in the past. The function is based in de creationtime attribute.
   The compressionlevel is default (Optimal). This function will compress all files in the folder!

   A combination of parameters will not work!
.EXAMPLE
   Compress-Files c:\logs\ 
   compresses all files in directory
.EXAMPLE
   Compress-Files c:\logs\ -delete
   compresses all files in directory and deletes them when are finished.
.EXAMPLE
   Compress-Files c:\logs\ -days 4
   compresses 4 days in the past. -delete also possible.
.EXAMPLE
   Compress-Files c:\logs\ -months 2
   compresses 2 months in the past. -delete also possible.
.EXAMPLE
   Compress-Files c:\logs\ -years 3
   compresses 3 years in the past. -delete also possible.
#>
function Compress-Files
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        # Sourcepath
        [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
        $SourcePath,

        # number of days to compress in past
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)]
        $Days,

        # number of months to compress in past
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=2)]
        $Months,

        # number of years to compress in past
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=3)]
        $Years,

        # delete switch, when set files will be removed.
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=4)]
        [switch]$Delete
       
    )
    

    #region variables
    $FullPath = $SourcePath + '*.*'
    $date = (get-date).ToString("yyyy-MM-dd")
    $yesterday = (get-date).AddDays(-1).ToString("yyyyMMdd")
    $lastmonth = (get-date).AddMonths(-1).ToString("yyyyMMdd")
    $lastyear = (get-date).AddYears(-1).ToString("yyyyMMdd")
    #endregion
    
    #checking the folders
    if (!(test-path -Path $SourcePath )){"$SourcePath folder does not exist."; break}

    #region Compress all files
    #compress files without any option
    if ($Days -eq $null -and $Months -eq $null -and $Years -eq $null){
        $files = Get-ChildItem $FullPath
        $DestinationPath = $SourcePath + $date + '.zip'
        $files | Compress-Archive -DestinationPath $DestinationPath -Update
        if ($delete){
            $files | % {Remove-Item $_ -Force}
        }
    }
    #endregion

    #region Compress days
    #Compress files based on days in the past.
    if($days -ne $null){
        $Days = '-' + $Days
        $days = (Get-Date).AddDays($Days).ToString("yyyyMMdd")
        $files = Get-ChildItem $FullPath | ? {$_.CreationTime.ToString("yyyyMMdd") -ge $Days -and $_.CreationTime.ToString("yyyyMMdd") -le $yesterday}
            
        foreach ($file in $files){
            $filename = ($file.CreationTime).ToString("yyyy-MM-dd")
            $DestinationPath = $SourcePath + $filename + '.zip'
            $file | Compress-Archive -DestinationPath $DestinationPath -update
            if ($delete){
                $files | % {Remove-Item $_ -Force}
            }
        }
    }
    #endregion

    #region Compress months
    #Compress files based on months in the past.
    if($months -ne $null){
        $Months = '-' + $Months
        $Months = (get-date).AddMonths($Months).ToString("yyyyMMdd")
        #$files = Get-ChildItem $FullPath | ? {$_.CreationTime -ge $Months -and $_.CreationTime -le $lastmonth}
        $files = Get-ChildItem $FullPath | ? {$_.CreationTime.ToString("yyyyMMdd") -ge $Months -and $_.CreationTime.ToString("yyyyMMdd") -le $lastmonth}

        foreach ($file in $files){
            $filename = ($file.CreationTime).ToString("yyyy-MM")
            $DestinationPath = $SourcePath + $filename + '.zip'
            $file | Compress-Archive -DestinationPath $DestinationPath -update
            if ($delete){
                Remove-Item $file -Force
            }
        }
    }
    #endregion

    #region Compress years
    #Compress files based on years in the past
    if ($Years -ne $null){
        $Years = '-' + $Years
        $Years = (get-date).AddYears($Years)
        $files = Get-ChildItem $FullPath | ? {$_.CreationTime.ToString("yyyyMMdd") -ge $Years -and $_.CreationTime.ToString("yyyyMMdd") -le $lastyear}

        foreach ($file in $files){
            $filename = ($file.CreationTime).ToString("yyyy")
            $DestinationPath = $SourcePath + $filename + '.zip'
            $file | Compress-Archive -DestinationPath $DestinationPath -update
            if ($delete){
                Remove-Item $file -Force
            }
        }
    }
    #endregion
}

 

Sander

https://cubik.com.tw/ click to find out more