9

如何使用标准 Windows 方法从命令行或 .bat 脚本启动具有关联的非默认命令(shell 动词)的文件,例如“编辑”、“打印”等。(在 Windows 资源管理器中右键单击文件时会在顶部提供的那些额外操作。)

从而得到效果

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

(ShellExecuteEx),但不使用 python、powershell 等额外工具。START 命令似乎没有提供。

4

4 回答 4

2

从评论和进一步搜索中了解到:在标准 Windows 中似乎确实没有针对该任务的直接命令。
但是,使用 VBScript 片段应该是高度兼容的并且具有最低的系统要求。(直接在此处的所有机器上工作 - 从 XP - 与 JScript 不同)

自 Windows 98 以来,Microsoft Windows 的每个桌面版本都默认安装了VBScript ;1自 Windows NT 4.0 Option Pack 起在 Windows Server 中;[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
于 2016-02-12T10:02:28.923 回答
1

一个例子来展示如何用单线做到这一点:

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

它将打开虚拟autoexec.bat文件,其中定义了用于编辑.bat文件的应用程序(默认情况下,记事本)。

于 2018-08-09T20:01:11.533 回答
0

可以使用批处理代码执行命令START执行的默认操作,即打开带有关联应用程序的文件。

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

文件名edit, printto, ... 必须指定为批处理文件的第一个参数。

@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

此批处理文件可能不适用于所有可能的操作命令,但它应该适用于所有edit, printto, ... 命令的 99.5%。

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

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg query /?
  • rem /?
  • set /?
  • setlocal /?
  • start /?
于 2016-02-13T15:34:41.310 回答
-1

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

START "" "Mypdf.pdf"
START "" "Myfile.txt"
START "" "Myjpg.jpg"
ETCETERA ETCETERA........
于 2016-02-10T13:57:23.980 回答