如何使用Powershell复制文件并保留原始时间戳 [英] how to use powershell to copy file and retain original timestamp

查看:183
本文介绍了如何使用Powershell复制文件并保留原始时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些文件或文件的文件夹从一个文件服务器复制到另一个。但是,我想保留原始时间戳和文件属性,以便新复制的文件具有与原始文件相同的时间戳。

I would like to copy some files or a folder of files from one file server to another. However, I want to keep the original timestamp and file attributes so that the newly copied files will have the same timestamp of the original files. Thanks in advance on any answer.

推荐答案

这是一个功能强大的功能,可以满足您的要求...绝对没有健全性检查,因此腔体清空器 ...

Here's a powershell function that'll do what you're asking... It does absolutely no sanity checking, so caveat emptor...

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
}

运行加载后,您可以执行以下操作:

Once you've run loaded that, you can do something like:

Copy-FileWithTimestamp foo bar

(您也可以将其命名为更短的名称,但是使用制表符完成,没什么大不了的……)

(you can also name it something shorter, but with tab completion, not that big of a deal...)

这篇关于如何使用Powershell复制文件并保留原始时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆