如果通过 Robocopy 复制的文件存在但修改日期不同,如何重命名

如果通过 Robocopy 复制的文件存在但修改日期不同,如何重命名

我正在使用 Robocopy 将文件从一个文件夹复制到另一个文件夹,如果文件存在于目标文件夹中并且时间戳不同,我想我的代码将覆盖该文件,我想要做的是如果文件存在但修改日期不同,则将目标文件夹中的复制文件重命名为 filename_timestamp.ext;如果文件存在但修改日期相同,则跳过它。

这是我当前的 Powershell 代码

$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

答案1

如上所述,由于 robocopy 不提供重命名功能。因此,您最终会得到如下结果:

$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
#>

相关内容