PowerShell 中的触摸功能 [英] touch Function in PowerShell

查看:59
本文介绍了PowerShell 中的触摸功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在 PowerShell 配置文件中添加了触摸功能

PS>记事本 $profile

function touch {Set-Content -Path ($args[0]) -Value ($null)}

保存并运行测试

touch myfile.txt

返回错误:

<前>touch :术语触摸"不被识别为 cmdlet、函数、脚本文件或可运行的程序.检查名称的拼写,或者是否有路径包含在内,请验证路径是否正确,然后重试.在行:1 字符:1+ 触摸我的文件+ ~~~~~+ CategoryInfo : ObjectNotFound: (touch:String) [], CommandNotFoundException+ FullQualifiedErrorId : CommandNotFoundException

解决方案

PowerShell 有函数的命名约定.强烈建议坚持使用它,如果只是为了停止收到有关它的警告,如果您将这些函数放在一个模块中并导入它.

可以找到关于命名转换的好读物这里.

话虽如此,Powershell 确实为您提供了别名功能,您可以在下面的函数中看到这一点.

正如 Jeroen Mostert 和其他人已经解释的那样,Touch 函数不是要破坏内容,而只是将 LastWriteTine 属性设置为当前日期.此函数允许您在参数 NewDate 中自己指定一个日期,但如果您省略它,它将默认为当前日期和时间.

function Set-FileDate {[CmdletBinding()]参数([参数(ValueFromPipeline = $true,Mandatory = $true,Position = 0)][字符串[]]$路径,[参数(强制= $false,位置= 1)][日期时间]$NewDate = (Get-Date),[开关]$Force)Get-Item $Path -Force:$Force |ForEach-Object { $_.LastWriteTime = $NewDate }}Set-Alias Touch Set-FileDate -Description 更新文件的 LastWriteTime"

现在,该函数有一个 PowerShell 不会反对的名称,但是通过使用 Set-Alias,您可以在代码中通过调用它来引用它 touch

I recently added a touch function in PowerShell profile file

PS> notepad $profile

function touch {Set-Content -Path ($args[0]) -Value ($null)}

Saved it and ran a test for

touch myfile.txt

error returned:

touch : The term 'touch' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
+ touch myfile
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (touch:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

解决方案

With PowerShell there are naming conventions for functions. It is higly recommended to stick with that if only to stop getting warnings about it if you put those functions in a module and import that.

A good read about naming converntion can be found here.

Having said that, Powershell DOES offer you the feature of Aliasing and that is what you can see here in the function below.

As Jeroen Mostert and the others have already explained, a Touch function is NOT about destroying the content, but only to set the LastWriteTine property to the current date. This function alows you to specify a date yourself in parameter NewDate, but if you leave it out it will default to the current date and time.

function Set-FileDate {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline = $true, Mandatory = $true, Position = 0)]
        [string[]]$Path,
        [Parameter(Mandatory = $false, Position = 1)]
        [datetime]$NewDate = (Get-Date),
        [switch]$Force
    )
    Get-Item $Path -Force:$Force | ForEach-Object { $_.LastWriteTime = $NewDate }
}
Set-Alias Touch Set-FileDate -Description "Updates the LastWriteTime for the file(s)"

Now, the function has a name PowerShell won't object to, but by using the Set-Alias you can reference it in your code by calling it touch

这篇关于PowerShell 中的触摸功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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