Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #REQUIRED: set the directory you want to compress.
  2. $dirToArchive = "C:\path\to\dir\"
  3.  
  4. #leave blank in order to automatically use directory name as archive name, otherwise enter the filename of the archive to create
  5. $archiveName = ""
  6. #$archiveName = "Data.7z"
  7.  
  8. #path to 7z.exe
  9. $exec = "C:\Program Files\7-Zip\7z.exe"
  10.  
  11. if ([string]::IsNullOrWhiteSpace($archiveName) )
  12. {
  13. $archiveName = (split-path -leaf $dirToArchive)+".7z"
  14. }
  15. $papadir = (Get-Item $dirToArchive ).Parent.FullName
  16. #archive is automatically put into parent directory of $dirToArchive
  17. $archiveName = Join-Path $papadir $archiveName
  18.  
  19. echo "Archive Path: $archiveName"
  20.  
  21. if (Test-Path "$archiveName")
  22. {
  23. echo "archive already exists"
  24. exit
  25. #del "$archiveName"
  26. }
  27.  
  28. $pw = Read-Host 'Password? Leave Blank to Not Use Encryption.'
  29.  
  30. $extList = get-childitem -r "$dirToArchive" | where Attributes -NotMatch "Directory" | %{$_.Extension.ToLower()} | select -unique | sort
  31. #loop through set of unique file extensions found in dir and add each one to archive (compressing or not compressing based on the extesnsion)
  32. Foreach ($i in $extList)
  33. {
  34. $str = $i.ToString()
  35. #change default compression level here (5=normal)
  36. $m = 5
  37. #for following extensions, Store but do not compress
  38. if ($str -in (".zip", ".7z", ".rar", ".gz", ".bz2", ".pdf", ".docx", ".accdb", ".xlsx", ".png", ".jpg", ".avi", ".mpg", ".mp3", ".m4v", ".m4a"))
  39. {
  40. $m = 0
  41. }
  42.  
  43. echo "Ext: $str m = $m"
  44.  
  45. $strWithExt = join-path "$dirToArchive" "*$str"
  46. $AllArgs = @('a', "-mx=$m", """$archiveName""", """$strWithExt""", "-r")
  47. #only add password param if user actually specified a password
  48. if ($pw -And $pw.Trim().Length -gt 0)
  49. {
  50. $AllArgs += "-p$pw"
  51. }
  52.  
  53. & "$exec" $AllArgs
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement