Solved

powershell script for archiving files

Posted on 2009-04-15
1
733 Views
Last Modified: 2012-06-27
Hello,

Can someone help me with a powershell script.
I need to do a recursive research on a folder and subfolder to find files that are older that 3 years (modified date) and then move these files to an other location.
I would like to respect the "tree" (folders and subfolders) and also delete the empty folders at the end.

Thank you for your help.
0
Comment
Question by:virginie8
1 Comment
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24157115

This should do it for you.

HTH

Chris


$SourceFolder = "C:\Source"

$DestinationFolder = "C:\Destination"
 

# Return all files in the structure which have not been written in the last 3 years
 

$Files = Get-ChildItem $SourceFolder -Recurse | ?{ 

  $_.PsIsContainer -eq $False -And `

  $_.LastWriteTime -lt ([DateTime]::Now).AddYears(-3) 

} | Select-Object Name, FullName, DirectoryName, LastWriteTime
 

# Move the files
 

$Files | %{

  $Destination = ($_.DirectoryName).Replace($SourceFolder, $DestinationFolder)

  If (!(Test-Path $Destination)) { [Void]New-Item -Path $Destination -Type Directory }

  (Get-Item $_.FullName).MoveTo("$Destination\$($_.Name)")

}
 

# Remove empty folders
 

$FoldersToDelete = Get-ChildItem $SourceFolder -Recurse | ?{ 

  $_.PsIsContainer -eq $True -And 

  (Get-ChildItem $_.FullName).Count -eq $Null 

}

$FoldersToDelete | %{ Remove-Item $_.FullName }

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
cPanel is a Unix based web hosting control panel that provides a graphical interface and automation tools designed to simplify the process of hosting a web site. cPanel utilizes a 3 tier structure that provides functionality for administrators, rese…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now