Skip to content

Instantly share code, notes, and snippets.

@timgaunt
Last active August 20, 2018 05:14
Show Gist options
  • Save timgaunt/f4b375eedb7900076082590e68d8134f to your computer and use it in GitHub Desktop.
Save timgaunt/f4b375eedb7900076082590e68d8134f to your computer and use it in GitHub Desktop.
function Compress-Subfolders
{
param
(
[Parameter(Mandatory = $true)][string] $InputFolder,
[Parameter(Mandatory = $true)][string] $OutputFolder
)
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$today = (get-date -Format yyyyMMdd)
$filter = 'u_ex' + (get-date -Format yyMMdd) + '.log'
$subfolders = Get-ChildItem $InputFolder | Where-Object { $_.PSIsContainer }
ForEach ($s in $subfolders)
{
$path = $s
Set-Location $path.FullName
$fullpath = $path.FullName
$pathName = $path.BaseName
#Get all items
$items = Get-ChildItem -exclude $filter
$zipname = $path.name + '_' + $today + ".zip"
$zippath = $outputfolder + $zipname
#Compress-Archive -Path $items -DestinationPath $zippath
Write-Host 'Writing: ' $zippath
sz a -mx=9 -sdel $zippath $items
#Remove-Item $items
Write-Host 'Done writing: ' $zippath
#Write-Host $items
}
}
# Usage
Compress-Subfolders -InputFolder "C:\inetpub\logs\LogFiles\" -OutputFolder "C:\Archive\"
function Compress-Subfolders
{
param
(
[Parameter(Mandatory = $true)][string] $InputFolder,
[Parameter(Mandatory = $true)][string] $OutputFolder
)
if (-not (test-path "$env:ProgramFiles\WinRAR\Rar.exe")) {throw "$env:ProgramFiles\WinRAR\Rar.exe needed"}
set-alias sz "$env:ProgramFiles\WinRAR\Rar.exe"
$today = (get-date -Format yyyyMMdd)
$subfolders = Get-ChildItem $InputFolder | Where-Object { $_.PSIsContainer }
ForEach ($path in $subfolders) {
$pathName = $path.BaseName
$fullpath = $path.FullName
$zipname = $path.name + '_' + $today + ".zip"
$zippath = $outputfolder + $zipname
Write-Host 'Writing: ' $zippath
sz a $zippath $fullpath -m5 -df -ep1 -WhatIf
Write-Host 'Done writing: ' $zippath
}
}
# Usage
Compress-Subfolders -InputFolder "C:\xxx\data\working\" -OutputFolder "C:\xxx\data\"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment