如何覆盖文件但保留原始文件时间戳

问题描述 投票:0回答:3

我有一个文件夹“目的地”,我有文件和他们的时间戳(创建,修改)我想保留当我用这些文件的更新版本覆盖其中一些文件。

Destination Folder 
File.mp4- Created 01/01/01 (size 500mb)

Source Folder 
File.mp4 - Created 12/12/12 (size 4.00gb)

我使用源文件夹中的新更新版本覆盖Destination文件夹中的文件,但将原始文件保留在目标文件夹时间戳中

结束结果(具有原始文件时间戳的目标文件夹中的新覆盖文件)

Destination Folder
File.mp4 - Created 01/01/01 (size 4.00gb)

我已经写了一个批处理文件来移动文件

  robocopy "Source" "Destination" /E

这可以完美地处理我的覆盖和移动文件,但我还没有找到保留时间戳的方法。我正在寻找命令行,所以我可以在批处理文件中实现它。任何帮助将不胜感激。我一直在阅读robocopy等的旗帜,但也许我读错了什么,但我还没弄明白。

编辑:这是我正在使用的powershell代码,但它还没有。

编辑2我删除了电源外壳,idk正在做什么。

windows powershell batch-file
3个回答
0
投票

你所追求的是/copyall旗帜。

robocopy "Source" "Destination" /e /copyall

这就像你有/copy:DATSOU一样

  • D数据
  • 属性
  • T时间戳
  • S NTFS访问控制列表(ACL)
  • O所有者信息
  • U审计信息

我建议你有一个read up on the flags以及它们是如何工作的。但是现在你有一个工作示例,可以根据需要交换标志!


0
投票

你可以使用System.IO.File类。它有许多你想要使用的方法,例如SetCreationTimeSetLastAccessTimeSetLastWriteTime等。像这样:

#Creating new file
$File = New-Item -Path 'C:\file.txt' -ItemType File
#Getting the file's LastWriteTime attribute
[datetime]$LastWriteTime = [System.IO.File]::GetLastWriteTime($File)
#30 августа 2018 г. 9:34:56
"Changing file's data" | Out-File $File -Append
#Checkin the file's LastWriteTime attribute
[System.IO.File]::GetLastWriteTime($File)
#30 августа 2018 г. 9:35:49
#Now setting LastWriteTime attribute
[System.IO.File]::SetLastWriteTime($File, $LastWriteTime)
#Checkin the file's LastWriteTime attribute again
[System.IO.File]::GetLastWriteTime($File)
#30 августа 2018 г. 9:34:56

0
投票

好吧,也许您希望在目标中尚不存在文件时保留源文件夹中文件的时间戳。所以:

  • 如果文件已存在于目标文件夹中,则从目标中存在的文件中复制并恢复时间戳
  • 如果目标中不存在该文件,则从源复制并设置副本上的时间戳以匹配源文件夹中原始的时间戳。

如果这是你需要的,这是我编辑的代码:

$sourceFolder      = '<YOUR SOURCE PATH HERE>'
$destinationFolder = '<YOUR DESTINATION PATH HERE>'

# get all the files in the source folder and loop through the list
Get-ChildItem -Path $sourceFolder -File | ForEach-Object {
    # combine the file name with the destination path
    $destinationFile = Join-Path $destinationFolder $_.Name
    # check if the file already exists in the destination
    if (Test-Path -Path $destinationFile -PathType Leaf) {
        # if exists, preserve the file time stamps of the existing file
        $existingFile   = Get-Item -Path $destinationFile
        $creationTime   = $existingFile.CreationTime
        $lastWiteTime   = $existingFile.LastWriteTime
        $lastAccessTime = $existingFile.LastAccessTime
    }
    else {
        # the file does not exist in the destination folder, so just copy
        # keep the timestamps from the source file? (Copy-Item will otherwise set a new CreationTime)
        $creationTime   = $_.CreationTime
        $lastWiteTime   = $_.LastWriteTime
        $lastAccessTime = $_.LastAccessTime
    }
    Copy-Item -Path $_.FullName -Destination $destinationFile -Force
    # restore the original timestamps on the newly copied file
    $newFile = Get-Item -Path $destinationFile
    $newFile.CreationTime   = $creationTime
    $newfile.LastWriteTime  = $lastWiteTime
    $newfile.LastAccessTime = $lastAccessTime
}
© www.soinside.com 2019 - 2024. All rights reserved.