我有一个文件夹“目的地”,我有文件和他们的时间戳(创建,修改)我想保留当我用这些文件的更新版本覆盖其中一些文件。
例
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正在做什么。
你所追求的是/copyall
旗帜。
robocopy "Source" "Destination" /e /copyall
这就像你有/copy:DATSOU
一样
我建议你有一个read up on the flags以及它们是如何工作的。但是现在你有一个工作示例,可以根据需要交换标志!
你可以使用System.IO.File类。它有许多你想要使用的方法,例如SetCreationTime
,SetLastAccessTime
,SetLastWriteTime
等。像这样:
#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
好吧,也许您希望在目标中尚不存在文件时保留源文件夹中文件的时间戳。所以:
如果这是你需要的,这是我编辑的代码:
$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
}