使用REG QUERY确定InstallPath

时间:2017-08-09 06:40:39

标签: batch-file registry

我正在使用批处理文件来确定使用InstallPath的R的REG QUERY

@echo off

REM get path
for /f "tokens=2*" %%a in ('REG QUERY "HKCU\Software\R-Core\R64" /v InstallPath') do set "RPath=%%~b"

set "var=\bin\R.exe --no-save"
set "R=%RPath%%var%"

REM start R fed with a script
%R% < "path.to.some.rfile.r"

这曾经完美地运行,直到我将R更新到版本3.4.1,它将InstallPath的密钥写入windows注册表中的子文件夹。

由于此子文件夹以R的版本命名,并且我希望批处理文件独立于R版本工作,因此我希望从任何现有的子文件夹中获取InstallPath。我怎么能这样做呢?

1 个答案:

答案 0 :(得分:1)

通过在执行query /s命令行工具时指定reg命令行开关,可以递归地查询注册表项及其子项的所有值。以下批处理脚本检索在注册表项InstallPath或其任何子项中找到的名为HKCU\Software\R-Core\R64的第一个注册表值的数据。

@echo off

set "key=hkcu\software\r-core\r64"
set "scr=path.to.some.rfile.r"
set "val=installpath"
set "bin=bin\r.exe"
set "arg=--no-save"
set "rPath="

:: Retrieve the installation directory path of R from the registry
for /f "tokens=2,*" %%i in ('reg query "%key%" /v "%val%" /s') do (
  if not defined rPath (
    set "rPath=%%~j"
  )
)
set "r=%rPath%\%bin% %arg%"

:: The contents of some script file is fed to the standard input stream of R 
%r% 0<"%scr%"

根据R的安装方式,您还可以尝试使用where命令检索R二进制文件的完全限定路径,而不是从注册表中查询值。

for /f "delims=" %%e in ('where r') do set "r=%%~e"
相关问题