有没有办法在 powershell 中使用相当于 touch 来更新文件的时间戳? [英] is there a way to use the equivalent of touch in powershell to update time stamps of a file?

查看:16
本文介绍了有没有办法在 powershell 中使用相当于 touch 来更新文件的时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用 unix 命令触摸"来更新系统中任何新获取/下载的文件的时间戳.很多时候,现有文件的时间戳是旧的,因此它可能会在系统中丢失.MS-Windows powershell 中有没有办法这样做.我有 powershell 5 和 powershell 6.

I use the unix command 'touch' a lot to update time-stamps of any new acquired/downloaded file in my system. Many a times the time-stamp of the existing file is old so it can get lost in the system. Is there a way in MS-Windows powershell to do so. I have both powershell 5 as well as powershell 6.

推荐答案

可以用

$file = Get-Item C:IntelLogsIntelCpHDCPSvc.log
$file.LastWriteTime = (Get-Date)

或者 CreationTime 或 LastAccessTime,如果您愿意的话.

Or CreationTime or LastAcccessTime, if you prefer.

作为一个函数

Function UpdateFileLastWriteTimeToToday() { 
        Param ($FileName)
    $file = Get-Item $FileName
    Echo "Current last write time: " $file.LastWriteTime
    $file.LastWriteTime = (Get-Date)
    Echo "New last write time: " $file.LastWriteTime
}

#How to use

UpdateFileLastWriteTimeToToday -FileName C:IntelLogsIntelGFX.Log

随着输出

Current last write time: 
Tuesday, April 16, 2019 1:09:49 PM

New last write time: 
Monday, November 4, 2019 9:59:55 AM

这篇关于有没有办法在 powershell 中使用相当于 touch 来更新文件的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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