Guest User

powershell zip archiver script

a guest
May 29th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Program Name: ZipArchiver
  2. #Programmer: Drake Owen
  3. #Description: Zips all files within certain parameters input by the user.
  4.  
  5. #Parameters
  6. param(
  7. [string]$FilePath,
  8. [string]$NameFilter, #Required #String
  9. [string]$DateFilterType, #Required #String
  10. [int]$DateFilterNum, #Required Number
  11. [string]$ArcPath,
  12. [string]$ArcName, #Required #String
  13. [string]$ArcFileFreq,
  14. [string]$ArcFileDateFormat
  15. )
  16.  
  17. #if required parameters are not passed program will close
  18. if (!$NameFilter -or !$DateFilterType -or !$ArcName -or !$DateFilterNum)
  19. {  
  20.     exit
  21. }
  22.  
  23. #Defines Default FilePath if not defined
  24. if (!$FilePath)
  25. {
  26.     $FilePath = ".\"
  27. }
  28.  
  29. #Converts days to weeks
  30. if ($DateFilterType -eq "TW" -or $DateFilterType -eq "PW")
  31.     {
  32.         $DateFilterNum = (7 * $DateFilterNum) + $DateFilterNum
  33.     }
  34.  
  35. #Declaring Variables
  36. $today = (get-date) #get today's date
  37. $fileList = (Get-ChildItem -path $FilePath -Filter $NameFilter) #gets list of files in directory
  38. $PSversion = $PSVersionTable.PSVersion | sort-object major | ForEach-Object     {$_.major}
  39.  
  40. #Set Target Date
  41. switch ($DateFilterType)
  42. {
  43.     TD {$targetdate = $today.AddDays(-$DateFilterNum)} #Total Days
  44.     TW {$targetdate = $today.AddDays(-$DateFilterNum)} #Total Weeks
  45.     TM {$targetdate = $today.AddMonths(-$DateFilterNum)} #Total Months
  46.     PD
  47.     {
  48.         $newdate = $today.AddDays(1) #Previous Day
  49.         $targetdate = $newdate.AddDays(-1 * $DateFilterNum)
  50.     }
  51.     PW
  52.     {
  53.         $newdate = New-Object System.DateTime $today.Year,$today.Month, 1 #Previous Month
  54.         $targetdate = $newdate.AddDays(1 + (-1 * $DateFilterNum))
  55.     }
  56.     PM
  57.     {
  58.         $newdate = New-Object System.DateTime $today.Year,$today.Month, 1 #Previous Month
  59.         $targetdate = $newdate.AddMonths(1 + (-1 * $DateFilterNum))
  60.     }
  61. }
  62.  
  63. #Loops Through All Files
  64. foreach($file in $fileList)
  65. {
  66.     #Have to reset DestPath each time
  67.     if(!$ArcPath)
  68.     {
  69.         $destPath = Join-Path -Path ".\" -Childpath $ArcName #Default destination filepath
  70.     }
  71.     else
  72.     {
  73.         $destPath = Join-Path -Path $ArcPath -Childpath $ArcName
  74.     }
  75.  
  76.     $dateFormat = $File.CreationTime.ToString($ArcFileDateFormat)
  77.  
  78.     #Naming the arc file based on date format
  79.         if ($ArcFileFreq -eq "Y" -or $ArcFileFreq -eq "M")
  80.     {
  81.         $destPath = [string]::Format($destPath, $dateFormat) #Formatting date of Arc File
  82.     }
  83.  
  84.     #If arc file is made quarterly
  85.     elseif ($ArcFileFreq -eq "Q")
  86.     {
  87.         #Calculating Quarter
  88.         switch ($file.CreationTime.Month) {
  89.             1 {$quarterNum = "1"}
  90.             2 {$quarterNum = "1"}
  91.             3 {$quarterNum = "1"}
  92.             4 {$quarterNum = "2"}
  93.             5 {$quarterNum = "2"}
  94.             6 {$quarterNum = "2"}
  95.             7 {$quarterNum = "3"}
  96.             8 {$quarterNum = "3"}
  97.             9 {$quarterNum = "3"}
  98.             10 {$quarterNum = "4"}
  99.             11 {$quarterNum = "4"}
  100.             12 {$quarterNum = "4"}
  101.             Default {$quarterNum = "1"}
  102.         }
  103.  
  104.         $destPath = [string]::Format($destPath, $dateFormat, $quarterNum) #Formating date of Arc File
  105.     }
  106.    
  107.     #This is what actually zips stuff
  108.     if($file.CreationTime -lt $targetdate)
  109.     {                                                                    
  110.         $FullPath = Join-Path -path $FilePath -Childpath $file #Defines a fullpath used for the compress and remove commands
  111.         if ($PSversion -lt 5)
  112.         {
  113.             #Zip fuction goes here.
  114.         }
  115.         else
  116.         {
  117.             compress-archive -Path $FullPath -Update -DestinationPath $destPath #This is what actually compresses and archives the files
  118.         }
  119.         Write-Host $file "Was Added to" $ArcName
  120.         #Remove-Item -Path $FullPath #Deletes files after being zipped
  121.     }
  122.     else
  123.     {
  124.         Write-Host $file "Was not zipped |" "Created on:"$file.CreationTime
  125.     }
  126.     Write-Host "------------------------------------------------------------"
  127. }
Add Comment
Please, Sign In to add comment