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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1831
  • Last Modified:

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
0
DTS-Tech
Asked:
DTS-Tech
  • 4
2 Solutions
 
Darius GhassemCommented:
You can use something like Robocopy ot Richcopy to copy all data off to another drive both of these have gui so you can use this without scripting

http://blogs.technet.com/b/ken/archive/2009/06/10/build-4-0-216-has-been-posted-to-the-microsoft-download-center.aspx
0
 
becraigCommented:
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

0
 
becraigCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
becraigCommented:
It seems I had an error in the script I posted:  updating below-

param ([string] $SourceFolder, [string] $DestFolder,  [string] $DDate);
if ($SourceFolder -eq "" -or $DestFolder -eq "" -or $DDate -eq "")
{ 
write-host "
`t You failed to specify correct syntax
`n
`t Usage: script.ps1 -sourcefolder c:\folder -destfolder c:\folder2 -ddate 40 
" -fore red
}
else
{
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))
	{
$FileName = $FileObj.Name
$fullpath = $FileObj.FullName
$copypath = ($fullpath -replace [regex]::Escape($SourceFolder), '')
$destpath = $DestFolder + $copypath

Copy-Item $fullpath $destpath
	}
				}
}

Open in new window


Give it a go ...
If you need any extra functionality like not copying files already present etc let me know.
0
 
becraigCommented:
Were you able to test this and did you have any additional questions ?
0
 
DTS-TechAuthor Commented:
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
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now