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

4

9

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.

Chanterelle answered 10/2, 2016 at 13:4 Comment(3)
Perhaps like this: #3207865Laurynlausanne
@Marged, RunDLL32 doesn't seem to work for ShellExecute(Ex) - as explained in the linked issue; I tried a little though ..Chanterelle
I don't understand why you exclude powershell and others like wsh. They are part of at least modern Windows and simplify what you are trying to do.Laurynlausanne
C
2

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
Chanterelle answered 12/2, 2016 at 10:2 Comment(0)
S
1

An example to show how to do it with an one-liner:

mshta vbscript:Execute("CreateObject(""shell.application"").ShellExecute""%SystemDrive%\autoexec.bat"","""","""",""edit"",1:close")

It will open the dummy autoexec.bat file with the application defined to edit .bat files (by default, Notepad).

Satisfaction answered 9/8, 2018 at 20:1 Comment(0)
O
0

It is possible to do with batch code what is done by command START for default action of opening a file with associated application.

In the commented batch code below the shell verb must be specified in third line being assigned to environment variable ActionCommand.

The name of the file to edit, printto, ... must be specified as first parameter of the batch file.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ActionCommand=edit"

rem Check if batch file was started with name of an existing file.

if "%~1" == ""      set "ErrMsg=No file name specified as argument on starting %~nx0" & goto OutputError
if exist "%~1\"     set "ErrMsg="%~f1" is a directory and not a file" & goto OutputError
if not exist "%~f1" set "ErrMsg=A file "%~f1" does not exist" & goto OutputError

rem Check if specified file has a file extension. Files starting with . and
rem not containing at least a second . are also files with no file extension.

if "%~n1" == "" set "ErrMsg=File "%~f1" has no file extension" & goto OutputError
if "%~x1" == "" set "ErrMsg=File "%~f1" has no file extension" & goto OutputError


rem On Windows Vista and later REG.EXE outputs without version info for example:

rem HKEY_CLASSES_ROOT\.txt
rem    (Default)    REG_SZ    txtfile

rem There are only spaces used to separate value name, value type and value string.

rem But REG.EXE version 3.0 outputs on Windows XP with version info for example:

rem ! REG.EXE VERSION 3.0
rem
rem HKEY_CLASSES_ROOT\.txt
rem     <NO NAME>   REG_SZ  txtfile

rem NOTE: There are 4 indent spaces and 2 separating tabs in REG 3.0 output line.

rem So either token 2 or token 3 contains value type REG_SZ
rem used to identify the line with the wanted information.
set "TypeToken=2"

rem Get name of registry key associated with extension of specified file.

:GetAssociatedKey
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe query "HKCR\%~x1" /ve 2^>nul') do (
    if "%%A" == "REG_SZ" set "KeyName=%%B" & goto GetCommand
    if "%%A" == "NAME>" set "TypeToken=3" & goto GetAssociatedKey
)
set "ErrMsg=No file assocation found for %~x1 in registry" & goto OutputError

:GetCommand
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe query "HKCR\!KeyName!\shell\%ActionCommand%\command" /ve 2^>nul') do (
    if "%%A" == "REG_SZ"        set "ActionCommand=%%B" & goto PrepareCommand
    if "%%A" == "REG_EXPAND_SZ" set "ActionCommand=%%B" & goto PrepareCommand
)
set "ErrMsg=No edit command found for %~x1 in registry" & goto OutputError

rem Replace "%1" or %1 by full name of specified file in double quotes or
rem append a space and full name of specified file if the command string
rem does not contain "%1" or %1 at all. Then expand the command string.

:PrepareCommand
set "ActionCommand=!ActionCommand:"%%1"="%~f1"!"
set "ActionCommand=!ActionCommand:%%1="%~f1"!"
if "!ActionCommand:%~f1=!" == "!ActionCommand!" set "ActionCommand=!ActionCommand! "%~f1""
call set "ActionCommand=%ActionCommand%"

rem Run the command with current directory set for the application to folder
rem of specified file without checking if the executable file exists at all.
rem Command start displays an error message box which must be confirmed by
rem the user by a click on button OK and outputs the error message also to
rem console if the executable to start could not be found.

start "" /D"%~dp1" %ActionCommand%

endlocal
goto :EOF


:OutputError
echo %~f0
echo.
echo Error: !ErrMsg!.
echo.
echo Press any key to exit batch processing ...
endlocal
pause >nul

This batch file might not work for all possible action commands, but it should work for 99.5% of all edit, printto, ... commands.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg query /?
  • rem /?
  • set /?
  • setlocal /?
  • start /?
Orelee answered 13/2, 2016 at 15:34 Comment(0)
C
-1

Not sure if this is what you are looking for, but using the START command opens the file I want to edit in the default program.

START "" "Mypdf.pdf"
START "" "Myfile.txt"
START "" "Myjpg.jpg"
ETCETERA ETCETERA........
Carrasco answered 10/2, 2016 at 13:57 Comment(5)
that just runs the default command. My question is about executing associated non-default actions - those which you get offered upon right-click on a file in the Explorer GUI. Like "edit", "print", etc.Chanterelle
@kxr, I believe that would be dependent on the program that is used to print that file. It would need a command line option to print the file. Thank you for editing your quesiton with your true intent.Carrasco
@Squasman, the question is independent of the filetype & program. Most file types offer the shell commands "open"/"run", "edit" and "print", and more can be defined by the user. So I want e.g. to execute (automated in a .bat) the registered "edit" command on a (any) file, whatever it is - and Windows knows how to call the right app with the right parameters. (The issue of non-default was always emphasized)Chanterelle
@kxr, I think the only way you are going to accomplish your goal is too use Powershell, Vbscript or Jscript as those scripting environments have much more access to the operating system then a batch file does.Carrasco
seems so - with (only) VBScript being ubiquitous hereChanterelle

© 2022 - 2024 — McMap. All rights reserved.