首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PowerShell错误消息中使用touch命令创建新文件

在PowerShell错误消息中使用touch命令创建新文件
EN

Stack Overflow用户
提问于 2015-09-08 02:00:18
回答 12查看 58K关注 0票数 24

我的桌面上有一个使用PowerShell创建的目录,现在我试图在其中创建一个文本文件。

我确实将目录更改为新目录,并键入了touch textfile.txt

这是我得到的错误消息:

代码语言:javascript
复制
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 file.txt
+ ~~~~~
+ CategoryInfo          : ObjectNotFound: (touch:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException`

为什么不起作用?我必须一直使用Git Bash吗?

EN

回答 12

Stack Overflow用户

回答已采纳

发布于 2015-09-08 07:41:19

如果您需要在touch中使用PowerShell命令,您可以定义一个做正确事情的函数™:

代码语言:javascript
复制
function touch {
  Param(
    [Parameter(Mandatory=$true)]
    [string]$Path
  )

  if (Test-Path -LiteralPath $Path) {
    (Get-Item -Path $Path).LastWriteTime = Get-Date
  } else {
    New-Item -Type File -Path $Path
  }
}

将该函数放入您的配置文件中,以便在启动PowerShell时可用。

touch定义为别名(New-Alias -Name touch -Value New-Item)在这里是行不通的,因为New-Item有一个强制参数-Type,并且不能在PowerShell别名定义中包含参数。

票数 30
EN

Stack Overflow用户

发布于 2015-12-06 01:10:54

如果使用Windows,则与Mac/Unix的触摸相同的命令是:New-Item textfile.txt -type file

票数 27
EN

Stack Overflow用户

发布于 2019-09-27 06:44:44

用于power shell中的单个文件创建:ni textfile.txt

对于同时创建多个文件:touch a.txt,b.html,x.js是linux命令

ni a.txt,b.html,x.js是windows power命令。

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32448174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档