Is it possible to launch a file's default editor from a batch file?

China☆狼群 提交于 2019-12-07 07:49:34

问题


Background:

We can use a combination of PATHEXT and the Windows file associations to do the Command Prompt equivalent of right-clicking a file in Explorer and clicking Open, e.g.:

C:\code\python> echo print "Hello, StackOverflow!" >hello.py

C:\code\python> hello
Hello, StackOverflow!

Similarly, I could use this to launch Photoshop by typing:

C:\art\source> StackOverflowLogo.PDF

The Actual Problem:

I need the command prompt equivalent of right-clicking the file in Explorer and selecting Edit instead.

With my hello(.py) above, that would likely bring up Python's Idle editor. However, I need a universal solution that uses the OS-level association for the file type. I can't assume it.

The simplest possible example of what I'd like to do would be this hypothetical EDIT.BAT file, which would do nothing but launch the editor for the given filename:

@InsertMagic /Here %1

Thanks! (I hope.)


Aaand... the solution:

Alex K. suggests Powershell, which is of course a great solution. So to write my EDIT.BAT above, I could do this:

@powershell -command "start -verb edit '%1'"

(That's slightly naïve, since there are potential quoting issues, but you get the idea.)

Cheers for the speedy answer. :)


回答1:


Those items on the right-click menu are called Shell Verbs, they can be invoked by name for a target file but not with any direct batch or windows command.

Win7/2k8r2 upwards you can use PowerShell to do it from your batch file:

powershell -command "start -verb edit 'C:\art\source\StackOverflowLogo.PDF'" 


来源:https://stackoverflow.com/questions/36011597/is-it-possible-to-launch-a-files-default-editor-from-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!