Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script Help - Moving old files

Posted on 2013-05-21
5
Medium Priority
?
145 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:Meir Rivkin
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 41

Accepted Solution

by:
footech earned 1000 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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

916 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