Wednesday, October 23, 2013

Powershell script to copy selected directories with use of robocopy


#The script to Archive files from one share to another depending from the file last write time (preserving the source directory structure and security)

$mySourceRoot="source directory"
$myDestRoot="destination directory"
set-location $mySourceRoot
$myDir=Get-ChildItem
$mySortDir=@()
foreach ($arg in $myDir)
{
   
    $myType=$arg.GetType()
    #write $arg.LastWriteTime
   
    if ($mytype.Name -eq "DirectoryInfo")
    {
        if ($arg.LastWriteTime -lt (get-date 2009-01-01))
        {
            write $arg.Name  $arg.lastWritetime
            $mySortDir=$mySortDir+$arg
        }
    }
}
set-location c:\windows\system32
foreach ($arg in $mySortDir)
{
    $myPath=$arg.FullName
    $myDestPath=$myPath.Replace($mySourceRoot,$myDestRoot)
    .\robocopy.exe $myPath $myDestPath /mir /sec /r:0 | Out-Null
}

No comments:

Post a Comment