我想将一些文件或文件文件夹从一个文件服务器复制到另一个文件服务器。但是,我希望保留原始时间戳和文件属性,以便新复制的文件具有与原始文件相同的时间戳。谢谢您的任何答复。
发布于 2014-02-06 04:15:58
这是一个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
(您也可以将其命名为更短的名称,但在选项卡完成后,没有什么大不了的.)
发布于 2014-02-06 05:03:51
下面是如何在时间戳、属性和权限上进行复制。
$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())
}
}
发布于 2014-02-06 04:00:16
如果您对两步解决方案没有意见,那么
尝试此技术将文件属性从一个文件复制到另一个文件。(我已经用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
https://stackoverflow.com/questions/21593625
复制相似问题