Search code examples
windowsbatch-filecommand-promptfile-association

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


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. :)


Solution

  • 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'"