How to launch Windows' RegEdit with certain path?

前端 未结 13 1034
盖世英雄少女心
盖世英雄少女心 2020-12-08 04:15

How do I launch Windows\' RegEdit with certain path located, like \"HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\8.0\", so I don\'t have to do the clic

相关标签:
13条回答
  • 2020-12-08 04:55

    Building on lionkingrafiki's answer, here's a more robust solution that will accept a reg key path as an argument and will automatically translate HKLM to HKEY_LOCAL_MACHINE or similar as needed. If no argument, the script checks the clipboard using the htmlfile COM object invoked by a JScript hybrid chimera. The copied data will be split and tokenized, so it doesn't matter if it's not trimmed or even among an entire paragraph of copied dirt. And finally, the key's existence is verified before LastKey is modified. Key paths containing spaces must be within double quotes.

    @if (@CodeSection == @Batch) @then
    :: regjump.bat
    @echo off & setlocal & goto main
    
    :usage
    echo Usage:
    echo   * %~nx0 regkey
    echo   * %~nx0 with no args will search the clipboard for a reg key
    goto :EOF
    
    :main
    rem // ensure variables are unset
    for %%I in (hive query regpath) do set "%%I="
    
    rem // if argument, try navigating to argument.  Else find key in clipboard.
    if not "%~1"=="" (set "query=%~1") else (
        for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0"') do (
            set "query=%%~I"
        )
    )
    
    if not defined query (
        echo No registry key was found in the clipboard.
        goto usage
    )
    
    rem // convert HKLM to HKEY_LOCAL_MACHINE, etc. while checking key exists
    for /f "delims=\" %%I in ('reg query "%query%" 2^>NUL') do (
        set "hive=%%~I" & goto next
    )
    
    :next
    if not defined hive (
        echo %query% not found in the registry
        goto usage
    )
    
    rem // normalize query, expanding HKLM, HKCU, etc.
    for /f "tokens=1* delims=\" %%I in ("%query%") do set "regpath=%hive%\%%~J"
    if "%regpath:~-1%"=="\" set "regpath=%regpath:~0,-1%"
    
    rem // https://stackoverflow.com/a/22697203/1683264
    >NUL 2>NUL (
        REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit"^
            /v "LastKey" /d "%regpath%" /f
    )
    
    echo %regpath%
    
    start "" regedit
    goto :EOF
    
    @end // begin JScript hybrid chimera
    // https://stackoverflow.com/a/15747067/1683264
    var clip = WSH.CreateObject('htmlfile').parentWindow.clipboardData.getData('text');
    
    clip.replace(/"[^"]+"|\S+/g, function($0) {
        if (/^\"?(HK[CLU]|HKEY_)/i.test($0)) {
            WSH.Echo($0);
            WSH.Quit(0);
        }
    });
    
    0 讨论(0)
提交回复
热议问题