Kelly Garcia
asked on
Find Files older than 6 months and zip them
Hi All,
I need to find files older than 6 months and zip them, is there a script some has I can use?
Thank you in advance,
Kay
I need to find files older than 6 months and zip them, is there a script some has I can use?
Thank you in advance,
Kay
ASKER
should use the -recurse, Get-ChildItem Path -File -Recurse
Also should I utilise the lastaccesstime instead of lastwritetime?
Also should I utilise the lastaccesstime instead of lastwritetime?
Up to you really. I don't much care when files were opened, when they were last written to is often the best metric from my point of view. It's all subjective though, you might key off CreationTime instead.
ASKER
I have this script:
what is the best way to add the compress-archive command to this script? also any suggestions for improvement?
$pathname = Read-Host "Please Enter Path Name: "
GCI $pathname -Recurse |
?{$_.LastAccessTime -le (Get-date).AddMonths(-1)} |
Select FullName,CreationTime,LastAccessTime,LastWriteTime |
Export-Csv "c:\$(($pathname -split "\\")[-1]).csv" -NTI
what is the best way to add the compress-archive command to this script? also any suggestions for improvement?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I am getting this error:
Exception calling "Write" with "3" argument(s): "Stream was too long."
At C:\Windows\system32\Window sPowerShel l\v1.0\Mod ules\Micro soft.Power Shell.Arch ive\Micros oft.PowerS hell.Archi ve.psm1:81 9 char:29
+ ... $destStream.Write($buffer, 0, $numberOfBytesRead)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException
also LastAccessTime -le (Get-date).AddMonths(-1) - does this means files accessed less than a month ago?
Exception calling "Write" with "3" argument(s): "Stream was too long."
At C:\Windows\system32\Window
+ ... $destStream.Write($buffer,
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException
also LastAccessTime -le (Get-date).AddMonths(-1) - does this means files accessed less than a month ago?
You're likely hitting the 2GB limit on the zip file. How much do you have in $files?
No, it means accessed a month or more ago. When comparing date, a date is considered less if it comes before the value on the right hand side of the expression. For example, yesterday is less than today.
No, it means accessed a month or more ago. When comparing date, a date is considered less if it comes before the value on the right hand side of the expression. For example, yesterday is less than today.
(Get-Date).AddDays(-1) -lt (Get-Date)
ASKER
also I need the zip files saved in the same path along with the same files name after that I need the non zipped file deleted. should I create a new entry on experts exchange?
ASKER
also files that are 1gb or more
That 2GB stream error needs figuring out first. Chances are it'll be best to move the files to a staging area so the entire directory can be zipped.
Let's start with that:
Let's start with that:
$path = 'C:\Source'
$stagingPath = 'D:\Destination'
Push-Location $path
Get-ChildItem $path -Recurse -File |
Where-Object LastAccessTime -le (Get-date).AddMonths(-1) |
ForEach-Object {
$relative = Resolve-Path $_.Directory -Relative
$destination = Join-Path $stagingPath $relative.TrimStart('.')
if (-not (Test-Path $destination)) {
$null = New-Item $destination -ItemType Directory
}
Copy-Item $_.FullName -Destination $destination
}
Pop-Location
Start with that and check the size of everything. If it works, Copy-Item can be change to Move-Item (possibly). Alternatively, we can do one pass to generate the list, archive things, and if that works, delete the items on the list.
> also files that are 1gb or more
It needs to be 7-zip then. The .NET components don't stand a chance. Not a big deal though, start with collecting the files together. Obviously for files that large the size of the staging area is kind of important.
It needs to be 7-zip then. The .NET components don't stand a chance. Not a big deal though, start with collecting the files together. Obviously for files that large the size of the staging area is kind of important.
ASKER
I have written the code below:
$pathname = "C:\Users\kabir.uddin"
$files = Get-ChildItem $pathname -Recurse -File |
Where-Object LastAccessTime -le (Get-date).AddMonths(-1)
foreach ($f in $files)
{
$Name = $f.fullname
#Compress-Archive $f -DestinationPath $path\$Name.zip
}
ASKER
I will create a new entry on experts exchange
ASKER
apologies on the code its $f.name not $f.fullname
Open in new window