Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows: start a file using a (non-default) shell verb like "edit" from .bat or command-line

How can I start a file with an associated non-default command (shell verb) like "edit", "print", ... from command-line or from a .bat script by using standard Windows means.
(Those extra actions which you get offered on top upon right-click on a file in the Windows Explorer.)

Thus getting the effect of

python -c "import os;os.startfile('somepic.png', 'edit')"

(ShellExecuteEx), but without using extra tools like python, powershell, or so. The START command does not seem to offer that.

like image 550
kxr Avatar asked Feb 10 '16 13:02

kxr


1 Answers

As learned from the comments and after further searching: there seems to be no direct command for that task in standard Windows indeed.
However using a VBScript snippet should be highly compatible and have lowest system requirements. (Works on all machines here directly - from XP - unlike JScript)

VBScript has been installed by default in every desktop release of Microsoft Windows since Windows 98;1 in Windows Server since Windows NT 4.0 Option Pack;[2] and optionally with Windows CE (depending on the device it is installed on).

Example script shellexec.vbs :

' shellexec.vbs : starts a file using a (non-default) shell verb like "EDIT"
' Usage: shellexec.vbs FILE VERB
' Example: shellexec.vbs demo.png EDIT
fn = WScript.Arguments(0)
cmd = WScript.Arguments(1)
Wscript.Echo "ShellExecute """ + cmd + """ on " + fn
CreateObject("shell.application").ShellExecute fn, "", "", cmd, 1

Use from command-line or batch-file:

shellexec.vbs demo.png EDIT

or:

cscript.exe //Nologo shellexec.vbs demo.png EDIT
like image 82
kxr Avatar answered Sep 17 '22 12:09

kxr