我想复制一些文件或一个文件夹从一个文件服务器到另一个文件服务器。但是,我想保留原始时间戳和文件属性,以便新复制的文件具有相同的原始文件时间戳。预先感谢任何答案。如何使用powershell复制文件并保留原始时间戳
5
A
回答
1
如果您对两步解决方案没问题;然后
- 首先拷贝从源文件在到dest
- 循环每个文件;并且对于每个文件
- 拷贝每个属性从源属性目的地
尝试这种技术来复制文件从一个文件属性到另一个。 (我已经用LastWriteTime说明了这一点;我相信你可以将它扩展为其他属性)。
#Created two dummy files
PS> echo hi > foo
PS> echo there > bar
# Get attributes for first file
PS> $timestamp = gci "foo"
PS> $timestamp.LastWriteTime
06 February 2014 09:25:47
# Get attributes for second file
PS> $t2 = gci "bar"
PS> $t2.LastWriteTime
06 February 2014 09:25:53
# Simply overwrite
PS> $t2.LastWriteTime = $timestamp.LastWriteTime
# Ta-Da!
PS> $t2.LastWriteTime
06 February 2014 09:25:47
3
这里有一个PowerShell的函数,会做什么你问...它绝对没有健全检查,所以买者自负 ...
function Copy-FileWithTimestamp {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0)][string]$Path,
[Parameter(Mandatory=$true,Position=1)][string]$Destination
)
$origLastWriteTime = (Get-ChildItem $Path).LastWriteTime
Copy-Item -Path $Path -Destination $Destination
(Get-ChildItem $Destination).LastWriteTime = $origLastWriteTime
}
一旦运行装载的是,你可以这样做:
Copy-FileWithTimestamp foo bar
(你也可以命名它的东西更短,但与标签完成,而不是什么大不了的事......)
0
这里是你如何能在时间戳,属性,并权限复制。
$srcpath = 'C:\somepath'
$dstpath = 'C:\anotherpath'
$files = gci $srcpath
foreach ($srcfile in $files) {
# Build destination file path
$dstfile = [io.FileInfo]($dstpath, '\', $srcfile.name -join '')
# Copy the file
cp $srcfile.FullName $dstfile.FullName
# Make sure file was copied and exists before copying over properties/attributes
if ($dstfile.Exists) {
$dstfile.CreationTime = $srcfile.CreationTime
$dstfile.LastAccessTime = $srcfile.LastAccessTime
$dstfile.LastWriteTime = $srcfile.LastWriteTime
$dstfile.Attributes = $srcfile.Attributes
$dstfile.SetAccessControl($srcfile.GetAccessControl())
}
}
相关问题
- 1. 上传到Amazon S3时如何保留并保留文件原始修改日期的时间戳?
- 2. 如何在检入到颠覆中时保留原始文件时间戳?
- 3. 如何使用Java FileChannel复制保留时间戳
- 4. cURL - 使用cURL来保存ftp文件并保留文件的原始时间戳
- 5. 如何保留原始文件发布zip压缩的时间戳
- 6. 复制多个文件并给出前缀并保留原始文件
- 7. 如何克隆存储库并保留文件时间戳?
- 8. 如何使用SVN保留文件的时间戳?
- 9. 如何在使用Mercurial时保留原始文件日期时间?
- 10. 如何正确复制表格并保留其原始键值
- 11. 复制到vim时如何保留原始程序格式?
- 12. 如何使rsync保留未更改文件的时间戳
- 13. FFmpeg的HLS保存原始时间戳
- 14. 如何复制文件和时间戳之间的2倍,并保留目录结构
- 15. .Net库在保留时间戳的同时移动/复制文件
- 16. Maven资源插件:如何将资源复制到目标文件夹并保留时间戳?
- 17. 如何保留时间戳当gsutil cp
- 18. 复制我的SQL Server数据库时保留时间戳
- 19. 使用NSDateFormatter保留原始时区
- 20. 如何通过保留原始换行符的行来复制文件行
- 21. 复制文件并保留副本
- 22. 如何删除原始文件并仅保留使用CarrierWave的版本?
- 23. 保留原始修改的时间戳,同时将新文件替换为jar文件
- 24. 如何在保存文件时保留原始文件的副本
- 25. 如何在Linux中保留原始顺序来复制行?
- 26. 使用时间戳保存文件
- 27. 时间戳使用PowerShell的
- 28. Django Python文件上传如何保留原始文件
- 29. python复制带时间戳的文件
- 30. 在使用合并排序时保留原始索引c
整洁。它在我测试Copy-FileWithTimestamp时起作用。 –
我还没有测试过它,但我希望它会失败壮观,如果你试图在复制中使用通配符,我写这个函数的方式... –
我只是想通过报告我尝试你的代码来帮助社区它的工作。我的意思并不是暗示我正在为生产网络提供背书。 –