【问题标题】:Windows: start a file using a (non-default) shell verb like "edit" from .bat or command-lineWindows:使用(非默认)shell 动词(如 .bat 或命令行中的“编辑”)启动文件
【发布时间】:2016-02-10 13:04:54
【问题描述】:

如何从命令行或从 .bat 脚本通过使用标准 Windows 意味着
(在 Windows 资源管理器中右键单击文件时会在顶部提供的额外操作。)

从而得到效果

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

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

【问题讨论】:

  • @Marged,RunDLL32 似乎不适用于 ShellExecute(Ex) - 如链接问题中所述;不过我试了一下..
  • 我不明白你为什么要排除 powershell 和其他类似 wsh 的东西。它们至少是现代 Windows 的一部分,可以简化您尝试做的事情。

标签: windows shell batch-file command-line shell-verbs


【解决方案1】:

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

VBScript has been 默认安装在每个桌面版本中 Microsoft Windows 自 Windows 98 起;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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      editprintto、...的文件名必须指定为批处理文件的第一个参数。

      @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 /?

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 只运行 default 命令。我的问题是关于执行相关的 non-default 操作 - 在资源管理器 GUI 中右键单击文件时提供的操作。比如“编辑”、“打印”等。
        • @kxr,我相信这将取决于用于打印该文件的程序。它需要一个命令行选项来打印文件。感谢您以您的真实意图编辑您的问题。
        • @Squasman,问题与文件类型和程序无关。大多数文件类型提供 shell 命令“打开”/“运行”、“编辑”和“打印”,用户可以定义更多。所以我想要例如在(任何)文件上执行(在 .bat 中自动执行)注册的“编辑”命令,无论它是什么 - 并且 Windows 知道如何使用正确的参数调用正确的应用程序。 (一直强调非默认的问题)
        • @kxr,我认为实现目标的唯一方法是使用 Powershell、Vbscript 或 Jscript,因为这些脚本环境比批处理文件更能访问操作系统。
        • 似乎如此 - (仅)VBScript 无处不在
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-02
        • 2021-12-11
        • 1970-01-01
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-27
        相关资源
        最近更新 更多