Solved

Script Help - Moving old files

Posted on 2013-05-21
5
129 Views
Last Modified: 2014-09-27
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
0
Comment
Question by:Phil Pearce
5 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39184044
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

0
 
LVL 39

Accepted Solution

by:
footech earned 250 total points
ID: 39185397
Not a scripting solution, but if you're interested have you tried robocopy with the /maxlad switch?  You should be able to just run something like
robocopy g:\sourcedir d:\destinatindir /E /XD CaseTracking Sage50 /MAXLAD:1460 /R:2 /W:1

Open in new window

0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 39185432
And of course, if you do indeed want to move, not just copy, as I think you indiocated, use the /MOVE or /MOV option with ROBOCOPY.

~bp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
A procedure for exporting installed hotfix details of remote computers using powershell
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

705 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

19 Experts available now in Live!

Get 1:1 Help Now