Windows:如何从.bat或命令行启动一个文件,使用非默认的shell动词(如“edit”)?

9
如何通过标准的 Windows 工具,从命令行或 .bat 脚本启动带有关联的非默认命令(shell verb),例如“编辑”,“打印”等。 (在 Windows Explorer 中右键单击文件时提供的额外操作。)
从而获得以下效果:
python -c "import os;os.startfile('somepic.png', 'edit')"

如何在不使用额外工具(如python,powershell等)的情况下使用ShellExecuteEx命令?START命令似乎不能提供这个功能。


也许像这样:https://dev59.com/z3A75IYBdhLWcg3wm6Qj - Marged
@Marged,RunDLL32似乎无法用于ShellExecute(Ex) - 正如链接问题中所解释的那样;我尝试了一下.. - kxr
2
我不理解为什么你要排除PowerShell和其他类似的WSH工具。它们是现代Windows至少一部分,并且可以简化你想要实现的功能。 - Marged
4个回答

2
从评论和进一步搜索中得知:实际上,在标准的 Windows 中似乎没有直接的命令来完成这项任务。
然而,使用 VBScript 片段应该具有高度兼容性和最低系统要求。(在这里所有机器上都可以直接使用 - 从 XP 开始 - 不像 JScript)

VBScript 自 Windows 98 起已经默认安装在每个桌面版 Microsoft Windows 上;1 在 Windows Server 自 Windows NT 4.0 Option Pack 起;[2] 并且在 Windows CE 上可选(取决于安装设备)。

示例脚本 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

使用命令行或批处理文件:
shellexec.vbs demo.png EDIT

或者:

cscript.exe //Nologo shellexec.vbs demo.png EDIT

1

一个示例,展示如何使用一行代码实现:

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

它将使用默认应用程序(默认情况下为记事本)打开虚拟的autoexec.bat文件,以编辑.bat文件。

0

使用批处理代码可以实现与命令START相同的默认操作,打开关联应用程序的文件。

在下面的批处理代码中,必须在第三行指定shell动作,并将其分配给环境变量ActionCommand

要编辑、打印等文件的名称必须作为批处理文件的第一个参数指定。

@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

这个批处理文件可能不适用于所有可能的操作命令,但它应该适用于99.5%的editprintto等命令。

为了理解使用的命令及其工作原理,请打开一个命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg query /?
  • rem /?
  • set /?
  • setlocal /?
  • start /?

-1

不确定这是否是您要寻找的内容,但使用START命令可以在默认程序中打开我想编辑的文件。

START "" "Mypdf.pdf"
START "" "Myfile.txt"
START "" "Myjpg.jpg"
ETCETERA ETCETERA........

1
这是关于执行相关的非默认操作的问题 - 这些操作是在资源管理器GUI中右键单击文件时提供的,例如“编辑”,“打印”等。而默认命令则已经可以直接运行。 - kxr
@kxr,我相信这将取决于用于打印该文件的程序。它需要一个命令行选项来打印该文件。感谢您编辑问题并表达真实意图。 - Squashman
1
@Squasman,这个问题与文件类型和程序无关。大多数文件类型都提供了shell命令“打开”/“运行”,“编辑”和“打印”,用户还可以定义更多的命令。因此,我想自动执行(在.bat文件中)注册的“编辑”命令,无论是什么文件类型 - Windows知道如何使用正确的参数调用正确的应用程序。 (非默认情况始终受到强调) - kxr
1
@kxr,我认为你实现目标的唯一方法是使用PowerShell、VBScript或JScript,因为这些脚本环境比批处理文件更能访问操作系统。 - Squashman
似乎是这样 - 只有 VBScript 在这里普及。 - kxr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接