Dateien im Ordner zippen, löschen und archivieren – Powershell

Dateien, die älter sind als x Tage, sollen innerhalb eines Ordner gezippt, gelöscht und archiviert werden.


<#
.SYNOPSIS
.
.DESCRIPTION
Find, delete and zip files in a folder.
.PARAMETER days
Check the last write time of a file. File is older than x days
.PARAMETER path
Path of the root folder of the files to be deleted.
.PARAMETER zip
Makes an archive containing all the files that were found.
.PARAMETER delete
Deletes the files that were found.
.PARAMETER archivePath
Specifies the path of the archive file. Standard path: C:tmparchive-"DATE".zip
.EXAMPLE
C:PS> .delete_old_files.ps1 -days 5 -path C:tmp -zip yes -delete yes -archivepath C:archives
Files that are older than 5 days from now on, root folder C:tmp, files will be zipped and deleted, the archive for the zip files will be in C:archives
Note that 7zip is a must have for zipping the files!
.NOTES
Author: Jens Braun
Date: Feb 04, 2016
#>

param(
[parameter(Mandatory=$true)]
[int]$days,
[parameter(Mandatory=$true)]
[string]$path,
[parameter(Mandatory=$false)]
[string]$zip,
[parameter(Mandatory=$false)]
[string]$delete,
[parameter(Mandatory=$false)]
[string]$archivePath
)

$sz="C:Program Files (x86)7-Zip7z.exe"
$z=1
$date = get-date -uformat "%Y-%m-%d"
#Soll gezippt werden?
if($zip -eq "yes"){
if(test-path "C:Program Files (x86)7-Zip7z.exe"){
Write-Host "7zip found"
Write-Host "Files will be zipped"
if($archivePath){
if(test-path $archivePath){
$archivePath= -join ($archivePath,"archive-",$date,".","zip")
Write-Host "archive to $archive"
}else{
$archivePath = "C:tmparchive-$date.zip"
Write-Host "Archive Path not found, using Standardarchive: $archivePath"
}
}else{
$archivePath = "C:tmparchive-$date.zip"
Write-Host "Archive Path not found, using Standardarchive: $archivePath"
}
}else{
Write-Warning "7zip not found"
$zip = "no"
}
}else{
Write-Host "Files will not be zipped"
$zip = "no"
}

#Soll gelöcht werden?
if($delete -eq "yes"){
Write-Host "Files will be deleted"
}else{
Write-Host "Files will not be deleted"
$delete="no"
}

#weder noch
if(($delete -eq "no") -and ($zip -eq "no")){
Write-Warning "Neither delete nor zipping, exiting!"
break
}

#path vorhanden?
if(!(test-path $path)){
write-warning "Archive Path not found"
break
}
#Nur Dateien, keine Ordner!
$files2delete = gci -path $path -recurse -force | ? -FilterScript {(($_.LastWriteTime -lt (get-date).adddays(-$days)) -and (! $_.PSIsContainer)) }

$Files2Remove = $files2delete.Count
foreach ($file in $files2delete){
Write-Progress -Activity "Working..." -status ("Files Processed $z of "+$Files2Remove.ToString()) -percentComplete ($z / $Files2Remove*100)
if($zip -eq "yes"){
& $sz a $archivePath $file.fullname -ssc >$null 2>&1
}

if($delete -eq "yes"){
remove-item $file.fullname -force -whatif
}

$z++
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s