Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种方法可以使用powershell中的touch来更新文件的时间戳?_Powershell_Unix_Windows 10_Touch - Fatal编程技术网

有没有一种方法可以使用powershell中的touch来更新文件的时间戳?

有没有一种方法可以使用powershell中的touch来更新文件的时间戳?,powershell,unix,windows-10,touch,Powershell,Unix,Windows 10,Touch,我经常使用unix命令“touch”来更新系统中任何新获取/下载文件的时间戳。很多时候,现有文件的时间戳是旧的,因此它可能会在系统中丢失。MS Windows powershell中是否有这样做的方法。我有powershell 5和powershell 6 您可以使用 $file = Get-Item C:\Intel\Logs\IntelCpHDCPSvc.log $file.LastWriteTime = (Get-Date) 或者CreationTime或LastAccessTime,如

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

您可以使用

$file = Get-Item C:\Intel\Logs\IntelCpHDCPSvc.log
$file.LastWriteTime = (Get-Date)
或者CreationTime或LastAccessTime,如果您愿意的话

作为一项功能

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:\Intel\Logs\IntelGFX.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
显示如何更新单个文件上次修改的时间戳

下面是函数
Touch文件的源代码
,该文件以PowerShell惯用方式实现Unix
Touch
实用程序提供的大部分功能,包括对
-WhatIf
的支持、详细输出和传递选项

它可以在Windows PowerShell(版本3或更高版本)和PowerShell核心中工作

例如,您可以将函数放入
$PROFILE
;致电
获取帮助触摸文件
获取帮助。
如果要定义别名,建议不要将其命名为
touch
,以免与类Unix平台上的本机
touch
实用程序混淆
例如,
Set Alias tf Touch File
可以工作

示例

# Sets the last-modified and last-accessed timestamps for all text files
# in the current directory to the current point in time.
Touch-File *.txt


# Creates files 'newfile1' and 'newfile2' and outputs information about them as 
# System.IO.FileInfo instances.
# Note the need to use "," to pass multiple paths.
Touch-File newfile1, newfile2 -PassThru


# Updates the last-modified and last-accessed timestamps for all text files
# in the current directory to midnight (the start of) of today's date.
Touch-File *.txt -DateTime (Get-Date).Date


# Sets the last-modified and last-accessed timestamps of all text files
# in the current directory back by 1 hour.
Get-Item *.txt | Touch-File -Offset '-1:0'


# Sets the last-modified and last-accessed timestamps of all files in the 
# current directory to the last-modified timestamp of the current directory.
Get-ChildItem -File | Touch-File -ReferencePath . '-0:1'

触摸文件
源代码:

注意

# Sets the last-modified and last-accessed timestamps for all text files
# in the current directory to the current point in time.
Touch-File *.txt


# Creates files 'newfile1' and 'newfile2' and outputs information about them as 
# System.IO.FileInfo instances.
# Note the need to use "," to pass multiple paths.
Touch-File newfile1, newfile2 -PassThru


# Updates the last-modified and last-accessed timestamps for all text files
# in the current directory to midnight (the start of) of today's date.
Touch-File *.txt -DateTime (Get-Date).Date


# Sets the last-modified and last-accessed timestamps of all text files
# in the current directory back by 1 hour.
Get-Item *.txt | Touch-File -Offset '-1:0'


# Sets the last-modified and last-accessed timestamps of all files in the 
# current directory to the last-modified timestamp of the current directory.
Get-ChildItem -File | Touch-File -ReferencePath . '-0:1'
  • 下面的功能也可用,今后只保留后者。假设您已经查看了链接代码以确保其安全(我个人可以向您保证,但您应该经常检查),您可以按照以下方式直接安装它:

    irm https://gist.github.com/mklement0/82ed8e73bb1d17c5ff7b57d958db2872/raw/Touch-File.ps1 | iex
    
  • Touch
    在PowerShell中不是一个被认可的动词,但还是被选中了, 因为所有被认可的动词都不能充分表达 这个命令

功能触摸文件{

我要把它放在哪里,我想一直使用它。我想要它,这样我就可以使用一个命令并给出filename.ext的路径,这样它就完成了所需的操作。我想更新时间戳,而不仅仅是获取时间戳:(我确实看到了这一点,但不知道如何在powershell中使用别名:(第一行获取$file对象。第二行将文件的LastWriteTime更新为当前日期和时间。我也将其作为函数添加了。很好,不过如果您要编写函数,我建议增强它以支持多个文件;至于函数名:我建议在
Update
之后加上连字符,以符合要求。)格式转换为PowerShell的命名约定;而且,“现在”可能比“今天”更好。至于参数名称:采用文件系统路径的参数按约定命名为
-path
(如果它支持通配符)或者
-LiteralPath
。我知道,您是否知道或相信触摸项目将包含在MS Windows powershell的近期内。如果有一个愿望列表或bugtracker或跟踪它的东西?@shirish:Windows powershell将看不到任何进一步的增强,但您可以在其上对powershell核心提出功能请求最好的办法是发布一个模块/脚本。如果你的问题还没有完全解决,请考虑回答或提供反馈。