How to launch Windows' RegEdit with certain pa

2020-02-02 08:35发布

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

What's the command line argument to do this? Or is there a place to find the explanation of RegEdit's switches?

12条回答
The star\"
2楼-- · 2020-02-02 09:02

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);
    }
});
查看更多
叼着烟拽天下
3楼-- · 2020-02-02 09:07

Here is one more batch file solution with several enhancements in comparison to the other batch solutions posted here.

It also sets string value LastKey updated by Regedit itself on every exit to show after start the same key as on last exit.

@echo off
setlocal EnableDelayedExpansion
set "RootName=Computer"

if not "%~1"=="" (
    set "RegKey=%~1"
    goto PrepareKey
)

echo/
echo Please enter the path of the registry key to open.
echo/
set "RegKey="
set /P "RegKey=Key path: "

rem Exit batch file without starting Regedit if nothing entered by user.
if "!RegKey!"=="" goto ExitBatch

:PrepareKey
rem Remove square brackets and double quotes from entered key path.
set "RegKey=!RegKey:"=!"
if "!RegKey!"=="" goto ExitBatch
set "RegKey=!RegKey:[=!"
if "!RegKey!"=="" goto ExitBatch
set "RegKey=!RegKey:]=!"
if "!RegKey!"=="" goto ExitBatch

rem Replace hive name abbreviation by appropriate long name.
set "Abbreviation=%RegKey:~0,4%"
if /I "%Abbreviation%"=="HKCC" (
    set "RegKey=HKEY_CURRENT_CONFIG%RegKey:~4%"
    goto GetRootName
)
if /I "%Abbreviation%"=="HKCR" (
    set "RegKey=HKEY_CLASSES_ROOT%RegKey:~4%"
    goto GetRootName
)
if /I "%Abbreviation%"=="HKCU" (
    set "RegKey=HKEY_CURRENT_USER%RegKey:~4%"
    goto GetRootName
)
if /I "%Abbreviation%"=="HKLM" (
    set "RegKey=HKEY_LOCAL_MACHINE%RegKey:~4%"
    goto GetRootName
)
if /I "%RegKey:~0,3%"=="HKU" (
    set "RegKey=HKEY_USERS%RegKey:~3%"
)

:GetRootName
rem Try to determine automatically name of registry root.
for /F "tokens=1,2*" %%K in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey"') do (
    if /I "%%K"=="LastKey" (
        for /F "delims=\" %%N in ("%%M") do set "RootName=%%N"
    )
)

rem Is Regedit already running?
%SystemRoot%\System32\tasklist.exe | %SystemRoot%\System32\findstr.exe /B /I /L regedit.exe >nul
if errorlevel 1 goto SetRegPath

echo/
echo Regedit is already running. Path can be set only when Regedit is not running.
echo/
set "Choice=N"
set /P "Choice=Kill Regedit (y/N): "
if /I "!Choice!"=="y" (
    %SystemRoot%\System32\taskkill.exe /IM regedit.exe >nul 2>nul
    goto SetRegPath
)
echo Switch to running instance of Regedit without setting entered path.
goto StartRegedit

:SetRegPath
rem Add this key as last key to registry for Regedit.
%SystemRoot%\System32\reg.exe add "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "%RootName%\%RegKey%" /f >nul 2>nul

:StartRegedit
start /B regedit.exe

:ExitBatch
endlocal

The enhancements are:

  1. Registry path can be passed also as command line parameter to the batch script.

  2. Registry path can be entered or pasted with or without surrounding double quotes.

  3. Registry path can be entered or pasted or passed as parameter with or without surrounding square brackets.

  4. Registry path can be entered or pasted or passed as parameter also with an abbreviated hive name (HKCC, HKCU, HKCR, HKLM, HKU).

  5. Batch script checks for already running Regedit as registry key is not shown when starting Regedit while Regedit is running already. The batch user is asked if running instance should be killed to restart it for showing entered registry path. If the batch user chooses not to kill Regedit, Regedit is started without setting entered path resulting (usually) in just getting Regedit window to foreground.

  6. The batch file tries to automatically get name of registry root which is on English Windows XP My Computer, on German Windows XP, Arbeitsplatz, and on Windows 7 just Computer. This could fail if the value LastKey of Regedit is missing or empty in registry. For this case please set the right root name in third line of the batch code.

查看更多
再贱就再见
4楼-- · 2020-02-02 09:10

This seems horribly out of date, but Registration Info Editor (REGEDIT) Command-Line Switches claims that it doesn't support this.

查看更多
Melony?
5楼-- · 2020-02-02 09:12

Use the following batch file (add to filename.bat):

REG ADD HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit /v LastKey /t REG_SZ /d Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Veritas\NetBackup\CurrentVersion\Config /f
START regedit

to replace:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Veritas\NetBackup\CurrentVersion\Config

with your registry path.

查看更多
戒情不戒烟
6楼-- · 2020-02-02 09:13

Copy the below text and save it as a batch file and run

@ECHO OFF
SET /P "showkey=Please enter the path of the registry key: "
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "%showkey%" /f
start "" regedit

Input the path of the registry key you wish to open when the batch file prompts for it, and press Enter. Regedit opens to the key defined in that value.

查看更多
闹够了就滚
7楼-- · 2020-02-02 09:13

Here is a simple PowerShell function based off of this answer above https://stackoverflow.com/a/12516008/1179573

function jumpReg ($registryPath)
{
    New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" `
                     -Name "LastKey" `
                     -Value $registryPath `
                     -PropertyType String `
                     -Force

    regedit
}

jumpReg ("Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run") | Out-Null

The answer above doesn't actually explain very well what it does. When you close RegEdit, it saves your last known position in HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit, so this merely replaces the last known position with where you want to jump, then opens it.

查看更多
登录 后发表回答