How rename files copied via Robocopy if it exist but with different modify date
5,699
As noted since robocopy does not provide rename. So you end up with something like this:
$src = 'D:\Temp\Src'
Get-ChildItem -Path $src |
Format-Table -AutoSize
# Results
<#
Directory: D:\Temp\Src
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 04-Jan-20 02:16 64 mytest - 2019.txt
-a---- 12-Oct-20 12:19 0 mytest.txt
#>
$dest = 'D:\temp\dest'
Get-ChildItem -Path $dest |
Format-Table -AutoSize
# Results
<#
Directory: D:\temp\dest
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 04-Jan-20 02:16 64 mytest - 2019.txt
-a---- 04-Jan-20 02:16 64 mytest.txt
#>
$log_file = "D:\temp\dest\backup_$(Get-Date -UFormat "%Y%m%dT%H%M%S").txt"
ForEach ($File in (Get-ChildItem -Path $src))
{
If (
Get-Item -Path $dest\$($File.Name) |
Where-Object -Property LastWriteTime -NE $File.LastWriteTime
)
{
$RenameItemSplat = @{
Path = (Get-ChildItem -Path $dest -Filter $File).FullName
NewName = "$($File.BaseName)_$(Get-Date -Format d)$($File.Extension)"
}
Rename-Item @RenameItemSplat
$null = robocopy $src $dest $File /S /TEE /log:$log_file
<#
# Or
Copy-Item -Path $File -Destination $dest |
Out-File -FilePath $log_file -Append
#>
}
Else {Write-Verbose -Message "No action was taken while processing $($File.FullName)" -Verbose}
}
# Results
<#
VERBOSE: No action was taken while processing D:\Temp\Src\mytest - 2019.txt
#>
Get-ChildItem -Path $dest |
Format-Table -AutoSize
# Results
<#
Directory: D:\temp\dest
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12-Oct-20 14:11 1091 backup_20201012T141108.txt
-a---- 04-Jan-20 02:16 64 mytest - 2019.txt
-a---- 12-Oct-20 12:19 0 mytest.txt
-a---- 04-Jan-20 02:16 64 mytest_12-Oct-20.txt
#>
$src = 'D:\Temp\Src'
Get-ChildItem -Path $src |
Format-Table -AutoSize
# Results
<#
Directory: D:\Temp\Src
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 04-Jan-20 02:16 64 mytest - 2019.txt
-a---- 12-Oct-20 12:19 0 mytest.txt
#>
Get-Content -Path $log_file
# Results
<#
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Monday, 12 October, 2020 14:11:09
Source : D:\Temp\Src\
Dest : D:\temp\dest\
Files : mytest.txt
Options : /TEE /S /DCOPY:DA /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
1 D:\Temp\Src\
New File 0 mytest.txt
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 0 1 0 0 0
Files : 1 1 0 0 0 0
Bytes : 0 0 0 0 0 0
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Ended : Monday, 12 October, 2020 14:11:09
#>
Related videos on Youtube
Author by
Islam Mohamed
Updated on September 18, 2022Comments
-
Islam Mohamed over 1 year
I'm using Robocopy to copy files from a folder to another, i think my code will overwrite the file if it was existing on the destination folder and the timestamp was different, what im trying to do is to rename the copied file in the destination folder to filename_timestamp.ext if the file was exist but with different modify date and skip it if it was exist but with the same modify date.
This is my current Powershell code
$src = "D:\Projects" $dest = "H:\Backups\Projects" $log_file = "H:\Backups\Logs\backup_" + (Get-Date -UFormat "%Y%m%dT%H%M%S") + ".txt" robocopy $src $dest /S /TEE /log:$log_file
-
harrymc over 3 yearsRobocopy can't do renaming. You will need to write a PowerShell script for that. For specific problems with the script, you may ask our help.
-
postanote over 3 yearsDitto, you can use robocopy, but for what you are after Copy-Item is just as prudent. Just use a ForLoop of the $src files, and Get-Item of those files of the $dest, to compare the LastWriteTime, then use robocopy or Copy-Item as normal for each file processed.
-