Hello,
I need a script to compress files from the last 30 days in one zip file. I have this code that do the same but create one file for every single file:
## Adding 7zip to environment variables.
$7zippath = "$env:ProgramFiles\7-Zip\7z.exe"
Set-Alias 7zip $7zippath
## Replace x with the number of days before which the files have to be compressed.
$LastWrite = (get-date).AddDays(-30)
$date = (Get-Date).ToString('yyyyMMdd')
## Replace Path and Filter as per your need.
$Files = Get-ChildItem -Path "D:\LOGS" -Recurse -File | Where-Object {$_.LastWriteTime -le $LastWrite}
ForEach ($File in $Files) {
"Compressing Log $File"
7zip a "$($File.fullname)$date.zip" "D:\LOGS\$File"
if((Test-Path "$($File.fullname)$date.zip"))
{
"Log Compression succeeded, so deleting D:\LOGS\$File"
Remove-Item -Path "D:\LOGS\$File"
}
else
{
"Log Compression FAILED, Please check parameters are passed correctly"
}
}
```I dont know how can i zip all this files in one zipfile and name it as the last day of the month.
Thanks,