Link to home
Start Free TrialLog in
Avatar of Phil Pearce
Phil PearceFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Script Help - Moving old files

Hi,

I'm wondering if someone could help me. I'm looking for assistance in script creation.

At the moment I have a drive full of files. What I'm going to do is create an archive drive.

What I would like to do is move all the files that were last accessed over 4 years ago to this new drive but maintain the directory structure. So they are moved to the same directory but in a new location.

I can locate the files using this script

get-childitem G:\ -Exclude CaseTracking,Sage50 -rec -ErrorAction SilentlyContinue | where {!$_.PSIsContainer -AND $_.lastAccessTime -lt (Get-Date).AddDays(-1460)} | select-object FullName, LastWriteTime | export-csv -notypeinformation -delimiter '|' -path c:\temp\file.csv

Open in new window


Just wondering if someone could help me modifying the script to help me achieve what I am after?


Thanks
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

change $sourceDir and $archiveDir before running the script.
cls
$archiveDir = "c:\delete_archive\"
$sourceDir = "g:\"
$files = get-childitem $sourceDir -Exclude CaseTracking,Sage50 -rec -ErrorAction SilentlyContinue | where {!$_.PSIsContainer -AND $_.lastAccessTime -lt (Get-Date).AddDays(-10)} | select-object FullName, LastWriteTime

$files | %{
	$file = $_.FullName.ToLower()
	$archive = $file.Replace($sourceDir.ToLower() , $archiveDir.ToLower())
	New-Item -ItemType directory -Path ([System.IO.Path]::GetDirectoryName($archive)) -ErrorAction SilentlyContinue
	Copy-Item $file -Destination $archive -Force 
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial