Solved

Script Help - Moving old files

Posted on 2013-05-21
5
135 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 40

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 53

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

820 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