Link to home
Start Free TrialLog in
Avatar of DTS-Tech
DTS-TechFlag for Australia

asked on

Moving files older than a certain date, but keeping folder structure

Hi team

I'm in need of a solution to move files older than 3months off the file server (Server 2008 (Not R2)) onto an external hard drive or network share. I'm by no means skilled in creating script commands, but from what I've read most of the solutions would mean removing the folders etc from the source when moving to the destination which isn't what I want to do.

I'm hoping when the files are moved, the file structure will effectively look identical in both locations making it easier to track down these files if required.

Is this something that can be done, or am I expecting too much?

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Darius Ghassem
Darius Ghassem
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
This might help:

param ([string] $SourceFolder, [string] $DestFolder,  [string] $DDate);


write-host $sourcefolder
write-host $destfolder
write-host $ddate

if (!(Test-Path $SourceFolder)) 
{ write-host "Source $sourcefolder does not exist!" }
if (!(Test-Path $DestFolder)) 
{ write-host  "Destination $destfolder does not exist!" }


$FileList = Get-ChildItem $SourceFolder -recurse
ForEach ($FileObj in $FileList) {
if ($fileobj.LastWriteTime -lt (Get-Date).AddDays(-$Ddate))
	{
write-host "$FileName = $FileObj.Name"
write-host "$fullpath = $FileObj.FullName"

$FileName = $FileObj.Name
$fullpath = $FileObj.FullName
write-host "$copypath = [regex]::Replace($Fullpath,'$SourceFolder','');"
$copypath = [regex]::Replace("$fullpath", "$SourceFolder",'');
$destpath = $DestFolder + $copypath

Copy-Item "$fullpath" "$destpath"


	}

				}
  

Open in new window

I forgot to add usage :

script name (whatever you save it as .ps1)
script.ps1 -sourcefolder c:\sourcefolder -destfolder c:\destfolder -ddate40

That should do it.
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
Were you able to test this and did you have any additional questions ?
Avatar of DTS-Tech

ASKER

Sorry for the delay in the update, I haven't moved the files yet, but at this stage I think I'll end up using the Richcopy application due to the GUI. Though I do appreciate the script you've written out