Windows:使用(非默认)shell动词(如.bat或命令行中的“编辑”)启动文件

我怎样才能用命令行或者.bat脚本中的“编辑”,“打印”等命令,通过使用标准的Windows方式来启动一个关联的非默认命令(shell动词)。
(在Windows资源pipe理器中右键单击某个文件时,可以获得的额外操作。)

从而得到的效果

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

(ShellExecuteEx),但不使用额外的工具,如Python,PowerShell等。 START命令似乎没有提供。

Solutions Collecting From Web of "Windows:使用(非默认)shell动词(如.bat或命令行中的“编辑”)启动文件"

从评论中学到的东西,经过进一步的搜索:在标准的Windows中似乎没有直接的任务。
但是,使用VBScript代码段应该高度兼容并且具有最低的系统要求。 (直接在所有的机器上工作 – 从XP – 不像JScript)

自Windows 98以来, VBScript已经默认安装在Microsoft Windows的每个桌面版本中; 1 Windows server自Windows NT 4.0选项包; [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 

不知道这是你在找什么,但使用START命令打开我想在默认程序中编辑的文件。

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

批处理代码可以通过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 

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

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

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