Feb 8th, 2014 under Programming PowerShell
To synchronize file timestamps (creation, last access and last write) of two directory trees.
$src = "\\some\src\path";
$dest = "Z:\some\dest\";
function Sync-FileTimes($src,$dest)
{
$src
ForEach ($srcitm in Get-ChildItem -Path $src)
{
$destitmpath = (Join-Path $dest $srcitm.Name);
if ($srcitm.PSIsContainer -eq $True)
{
Sync-FileTimes $srcitm.FullName $destitmpath
}
else
{
# Check if dest file exists
if ((Test-Path $destitmpath) -eq $False)
{
Write-Error ("Destination item not found {0}" -f $destitmpath)
continue;
}
# Compare times
$destitm = Get-Item -Path $destitmpath;
if ($srcitm.CreationTime -ne $destitm.CreationTime)
{
$destitm.CreationTime = $srcitm.CreationTime;
}
if ($srcitm.LastAccessTime -ne $destitm.LastAccessTime)
{
$destitm.LastAccessTime = $srcitm.LastAccessTime;
}
if ($srcitm.LastWriteTime -ne $destitm.LastWriteTime)
{
$destitm.LastWriteTime = $srcitm.LastWriteTime;
}
}
}
}
Sync-FileTimes $src $dest