How can I get the value of a registry key using a batch script?

三世轮回 提交于 2019-12-06 11:59:05
for /f "tokens=3*" %%x in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\devenv.exe"') do set DEVENV="%%x %%y"
%DEVENV% /Command "Edit.Goto 83" "E:\examples.A.cpp"

One caveat - if you have more than one edition of VS installed, this will launch the version which was most recently installed.

More generically:

set REGKEY="HKLM\SOFTWARE\Wow6432Node\BI\Science\AB\exenamehere.exe"
set CPPFILE=C:\SomePathHere\foo.cpp

for /f "tokens=3*" %%x in ('reg query "%REGKEY%"') do set EXE="%%x %%y"
%EXE% /AnyOtherParamsHere "%CPPFILE%"

You could also accept a command line argument like so (ideally there would be error handling as well). %1 is the first argument, %2 would be the second, on up through 9. Taking more than 9 parameters is certainly possible, but is beyond the scope of this question.

set REGKEY="HKLM\SOFTWARE\Wow6432Node\BI\Science\AB\exenamehere.exe"
set FILENAME=%1

for /f "tokens=3*" %%x in ('reg query "%REGKEY%"') do set EXE="%%x %%y"
%EXE% /AnyOtherParamsHere %FILENAME%

If the path to your file has spaces in it, you'll need to quote them when you run this batch file (e.g., mybatch.cmd "C:\path with spaces\foo.cpp").

Also, don't forget to mark this as the answer if you've found it useful. :-)

@ECHO OFF
SETLOCAL 
FOR /F "tokens=2*" %%A IN (
   'REG QUERY "HKLM\SSOFTWARE\Microsoft\Windows\CurrentVersion\App Paths" /v devenv.exe'
) DO (set vs9dir=%%B)
ECHO %vs9dir%

should return your value - in theory. I can't verify as I don't have VS.

You can try this, it might work:

for /f "tokens=3*" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\devenv.exe"^|find "<NO NAME>"') do set "key=%%j"
echo "%key%"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!