Windows: start a file using a (non-default) shell verb like “edit” from .bat or command-line

前端 未结 4 796
醉话见心
醉话见心 2021-02-15 16:20

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 usi

4条回答
  •  粉色の甜心
    2021-02-15 16:32

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

提交回复
热议问题