powershell - 如何使用powershell复制文件并保留原始时间戳

标签 powershell copy timestamp

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

最佳答案

这是一个 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

(您也可以将其命名为更短的名称,但使用选项卡完成,没什么大不了的......)

关于powershell - 如何使用powershell复制文件并保留原始时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593625/

相关文章:

Javascript - 跟随链接,复制 URL 并更改原件?

java - (Java) ArrayList<obj> 中 add(obj o) 方法的内部工作

timestamp - unix 时间戳是存储时间戳的最佳方式吗?

javascript - 想要在 Node JS/MySQL 中将 12 小时时间字符串转换为时间对象

windows - 将整个脚本输出重定向到文件并同时在控制台中显示

powershell - bat 文件禁止用户输入

python - 为什么 np.ndarray.__deepcopy__ 需要一个多余的参数?

python - 从Windows Server 2008中的powershell 3.0脚本获取python版本

powershell - 如何从 Powershell/其他脚本设置 Bamboo 变量?

mysql - cakePHP 3.0 在保存时忽略实体访问器