2014-02-06 35 views

回答

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

整洁。它在我测试Copy-FileWithTimestamp时起作用。 –

+0

我还没有测试过它,但我希望它会失败壮观,如果你试图在复制中使用通配符,我写这个函数的方式... –

+0

我只是想通过报告我尝试你的代码来帮助社区它的工作。我的意思并不是暗示我正在为生产网络提供背书。 –

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()) 
    } 
} 
相关问题