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
Creating new file with touch command in PowerShell error message
提问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 touch
in PowerShell you could define a function that does The Right Thing™:
如果您需要touch
PowerShell 中的命令,您可以定义一个执行 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 touch
as an alias (New-Alias -Name touch -Value New-Item
) won't work here, because New-Item
has a mandatory parameter -Type
and 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, touch
is 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 $null
you'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.js
is the linux commandni a.txt,b.html,x.js
is the windows power shell command
同时创建多个文件:
touch a.txt,b.html,x.js
linux命令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-Item
with the Name
, ItemType
, and Path
parameters worked for me. In my case, to create a _netrc
file for git:
的组合New-Item
用Name
,ItemType
和Path
对我来说有效参数。就我而言,要_netrc
为 git创建一个文件:
New-Item -Name _netrc -ItemType File -Path $env:userprofile
References
参考