Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Program Name: ZipArchiver
- #Programmer: Drake Owen
- #Description: Zips all files within certain parameters input by the user.
- #Parameters
- param(
- [string]$FilePath,
- [string]$NameFilter, #Required #String
- [string]$DateFilterType, #Required #String
- [int]$DateFilterNum, #Required Number
- [string]$ArcPath,
- [string]$ArcName, #Required #String
- [string]$ArcFileFreq,
- [string]$ArcFileDateFormat
- )
- #if required parameters are not passed program will close
- if (!$NameFilter -or !$DateFilterType -or !$ArcName -or !$DateFilterNum)
- {
- exit
- }
- #Defines Default FilePath if not defined
- if (!$FilePath)
- {
- $FilePath = ".\"
- }
- #Converts days to weeks
- if ($DateFilterType -eq "TW" -or $DateFilterType -eq "PW")
- {
- $DateFilterNum = (7 * $DateFilterNum) + $DateFilterNum
- }
- #Declaring Variables
- $today = (get-date) #get today's date
- $fileList = (Get-ChildItem -path $FilePath -Filter $NameFilter) #gets list of files in directory
- $PSversion = $PSVersionTable.PSVersion | sort-object major | ForEach-Object {$_.major}
- #Set Target Date
- switch ($DateFilterType)
- {
- TD {$targetdate = $today.AddDays(-$DateFilterNum)} #Total Days
- TW {$targetdate = $today.AddDays(-$DateFilterNum)} #Total Weeks
- TM {$targetdate = $today.AddMonths(-$DateFilterNum)} #Total Months
- PD
- {
- $newdate = $today.AddDays(1) #Previous Day
- $targetdate = $newdate.AddDays(-1 * $DateFilterNum)
- }
- PW
- {
- $newdate = New-Object System.DateTime $today.Year,$today.Month, 1 #Previous Month
- $targetdate = $newdate.AddDays(1 + (-1 * $DateFilterNum))
- }
- PM
- {
- $newdate = New-Object System.DateTime $today.Year,$today.Month, 1 #Previous Month
- $targetdate = $newdate.AddMonths(1 + (-1 * $DateFilterNum))
- }
- }
- #Loops Through All Files
- foreach($file in $fileList)
- {
- #Have to reset DestPath each time
- if(!$ArcPath)
- {
- $destPath = Join-Path -Path ".\" -Childpath $ArcName #Default destination filepath
- }
- else
- {
- $destPath = Join-Path -Path $ArcPath -Childpath $ArcName
- }
- $dateFormat = $File.CreationTime.ToString($ArcFileDateFormat)
- #Naming the arc file based on date format
- if ($ArcFileFreq -eq "Y" -or $ArcFileFreq -eq "M")
- {
- $destPath = [string]::Format($destPath, $dateFormat) #Formatting date of Arc File
- }
- #If arc file is made quarterly
- elseif ($ArcFileFreq -eq "Q")
- {
- #Calculating Quarter
- switch ($file.CreationTime.Month) {
- 1 {$quarterNum = "1"}
- 2 {$quarterNum = "1"}
- 3 {$quarterNum = "1"}
- 4 {$quarterNum = "2"}
- 5 {$quarterNum = "2"}
- 6 {$quarterNum = "2"}
- 7 {$quarterNum = "3"}
- 8 {$quarterNum = "3"}
- 9 {$quarterNum = "3"}
- 10 {$quarterNum = "4"}
- 11 {$quarterNum = "4"}
- 12 {$quarterNum = "4"}
- Default {$quarterNum = "1"}
- }
- $destPath = [string]::Format($destPath, $dateFormat, $quarterNum) #Formating date of Arc File
- }
- #This is what actually zips stuff
- if($file.CreationTime -lt $targetdate)
- {
- $FullPath = Join-Path -path $FilePath -Childpath $file #Defines a fullpath used for the compress and remove commands
- if ($PSversion -lt 5)
- {
- #Zip fuction goes here.
- }
- else
- {
- compress-archive -Path $FullPath -Update -DestinationPath $destPath #This is what actually compresses and archives the files
- }
- Write-Host $file "Was Added to" $ArcName
- #Remove-Item -Path $FullPath #Deletes files after being zipped
- }
- else
- {
- Write-Host $file "Was not zipped |" "Created on:"$file.CreationTime
- }
- Write-Host "------------------------------------------------------------"
- }
Add Comment
Please, Sign In to add comment