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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32448174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:34:57  来源:igfitidea点击:

Creating new file with touch command in PowerShell error message

gitpowershell

提问by Antoni Parellada

I have a directory on my desktop created using PowerShell, and now I'm trying to create a text file within it.

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

I did change directory to the new one, and typed touch textfile.txt.

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

This is the error message I get:

这是我收到的错误消息:

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`

Why is it not working? Will I have to use Git Bash all the time?

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

回答by Ansgar Wiechers

If you need a command touchin PowerShell you could define a function that does The Right Thing™:

如果您需要touchPowerShell 中的命令,您可以定义一个执行 The Right Thing™ 的函数:

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
  }
}

Put the function in your profileso that it's available whenever you launch PowerShell.

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

Defining touchas an alias (New-Alias -Name touch -Value New-Item) won't work here, because New-Itemhas a mandatory parameter -Typeand you can't include parameters in PowerShell alias definitions.

定义touch为别名 ( New-Alias -Name touch -Value New-Item) 在这里不起作用,因为它New-Item有一个强制参数-Type,您不能在 PowerShell 别名定义中包含参数。

回答by Ryan Chase

If you're using Windows Powershell, the equivalent command to Mac/Unix's touch is: New-Item textfile.txt -type file.

如果您使用的是 Windows Powershell,则 Mac/Unix touch 的等效命令是:New-Item textfile.txt -type file.

回答by briantist

As Etan Reisnerpointed out, touchis not a command in Windows nor in PowerShell.

正如Etan Reisner指出的那样,touch在 Windows 和 PowerShell 中都不是命令。

If you want to just create a new file quickly (it looks like you're not interested in the use case where you just update the date of an existing file), you can use these:

如果您只想快速创建一个新文件(看起来您对仅更新现有文件的日期的用例不感兴趣),您可以使用这些:

$null > textfile.txt
$null | sc textfile.txt

Note that the first one will default to Unicode, so your file won't be empty; it will contain 2 bytes, the Unicode BOM.

请注意,第一个将默认为 Unicode,因此您的文件不会为空;它将包含 2 个字节,即Unicode BOM

The second one uses sc(an alias for Set-Content), which defaults to ASCII when used on the FileSystem.

第二个使用sc( 的别名Set-Content),在 FileSystem 上使用时默认为 ASCII

If you use an empty string (''or ""or [String]::Empty) instead of $nullyou'll end up with a line break also.

如果您使用的是空字符串(''""[String]::Empty),而不是$null你会得到一个换行符也结束了。

回答by M2395

For single file creation in power shell : ni textfile.txt

在 power shell 中创建单个文件: ni textfile.txt

For multiple files creation at a same time: touch a.txt,b.html,x.jsis the linux command
ni a.txt,b.html,x.jsis the windows power shell command

同时创建多个文件: touch a.txt,b.html,x.jslinux命令
ni a.txt,b.html,x.js是windows power shell命令

回答by Gerard

cd into the your path where you want the file to be created and then type...

cd 进入要创建文件的路径,然后键入...

New-item -itemtype file yourfilenamehere.filetype

New-item -itemtype 文件 yourfilenamehere.filetype

example

例子

New-item -itemtype file index.js

New-item -itemtype 文件 index.js

回答by Paul Sweatte

The combination of New-Itemwith the Name, ItemType, and Pathparameters worked for me. In my case, to create a _netrcfile for git:

的组合New-ItemNameItemTypePath对我来说有效参数。就我而言,要_netrc为 git创建一个文件:

 New-Item -Name _netrc -ItemType File -Path $env:userprofile

References

参考