We help IT Professionals succeed at work.

Find Files older than 6 months and zip them

Kelly Garcia
Kelly Garcia asked
on
703 Views
Last Modified: 2018-02-15
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
Comment
Watch Question

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
This depends a bit on your PowerShell versions. Compress-Archive is new(ish).
$files = Get-ChildItem Path -File | Where-Object LastWriteTime -lt (Get-Date).AddMonths(-6) | Select-Object -ExpandProperty FullName
Compress-Archive $files -DestinationPath something.zip

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
should use the -recurse, Get-ChildItem Path -File  -Recurse

Also should I utilise the lastaccesstime instead of lastwritetime?
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.
Kelly GarciaSenior Systems Administrator

Author

Commented:
I have this script:


$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 

Open in new window


what is the best way to add the compress-archive command to this script? also any suggestions for improvement?
PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Kelly GarciaSenior Systems Administrator

Author

Commented:
I am getting this error:

Exception calling "Write" with "3" argument(s): "Stream was too long."
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:819 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?
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.
(Get-Date).AddDays(-1) -lt (Get-Date)

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
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?
Kelly GarciaSenior Systems Administrator

Author

Commented:
also files that are 1gb or more
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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:
$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

Open in new window

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.
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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.
Kelly GarciaSenior Systems Administrator

Author

Commented:
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
		}

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
I will create a new entry on experts exchange
Kelly GarciaSenior Systems Administrator

Author

Commented:
apologies on the code its $f.name not $f.fullname

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions