windows 如何在cmd中使用默认应用程序打开文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3729187/
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 07:36:48  来源:igfitidea点击:

How to open file with default application in cmd?

windowsbatch-filecmd

提问by omnix

I'm trying to open a file in it's default editor after the user has created the file. So far my script is:

在用户创建文件后,我试图在它的默认编辑器中打开一个文件。到目前为止,我的脚本是:

@echo off
@echo --- Create A New File ---
@echo -
@echo Where should we put the new file?
set /p fileLocation=@ %UserProfile%\
@echo -
@echo What do you want to call your new file?
set /p fileName=@ 
@echo -
@echo Almost Done! What is the files extension?
set /p extension=@ .
@echo -
copy NUL "%UserProfile%\%fileLocation%\%fileName%.%extension%"

(ignore the extra echos and '@' those are just for fun)

(忽略额外的回声和“@”只是为了好玩)

After I click the file, it does the command: Choose Location > Choose File Name > Choose File extension. I'm almost done on what I want but theres one last thing. How can I get the file name that I created and then open in its default text-editor?

单击该文件后,它会执行以下命令:Choose Location > Choose File Name > Choose File extension. 我几乎完成了我想要的,但还有最后一件事。如何获取我创建的文件名,然后在其默认文本编辑器中打开?

回答by Colin Hebert

You can use startto open the file with the associated application.

您可以使用start关联的应用程序打开文件。



Resources :

资源 :

回答by Oliver

In windows you can use start(http://ss64.com/nt/start.html).

在 Windows 中,您可以使用start( http://ss64.com/nt/start.html)。

start "" "%UserProfile%\%fileLocation%\%fileName%.%extension%"

回答by Nathan

You can also use explorer.exe/explorerto open the file (e.g. explorer file.txt). This also works nicely if you use WSL, especially with an alias like alias open="explorer.exe"so you can just call it like, e.g., open file.txt.

您还可以使用explorer.exe/explorer打开文件(例如explorer file.txt)。如果您使用 WSL,这也能很好地工作,尤其是使用像alias open="explorer.exe"这样的别名,这样您就可以像这样称呼它,例如open file.txt.

回答by T.Todua

I achieved the correct way of FILE ASSOCIATION using these cmd commands. this is just an example:

我使用这些 cmd 命令实现了正确的 FILE ASSOCIATION 方法。这只是一个例子:

REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v @ /t REG_SZ /d "\"C:\Program Files\Noteepad++\notepad++.exe\" \"%1\"" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt" /v "Application" /t REG_SZ /d "notepad++.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList" /v "g" /t REG_SZ /d "notepad++.exe" /f

assoc .txt=MyCustomType
ftype MyCustomType="C:\Program Files\Noteepad++\notepad++.exe" "%1"

(it's better to put them in .bat file)

(最好把它们放在 .bat 文件中)