smoff
smoff

Reputation: 648

Determine InstallPath using REG QUERY

I am using a batch file to determine the InstallPath of R using 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"

This used to work perfectly until I updated R to version 3.4.1 which writes the key of InstallPath to a subfolder in the windows registry.

Since this subfolder is named by the version of R and I want the batch file to work independently from the R-version, I want to get InstallPath from any existing subfolder. How could I mangage to do that?

Upvotes: 0

Views: 1384

Answers (1)

303
303

Reputation: 4732

You could query all values of a registry key and its subkeys recursively by specifying the query /s command-line switch when executing the reg command-line tool. The following batch-script retrieves the data of the first registry value found named InstallPath in registry key HKCU\Software\R-Core\R64 or any of its subkeys.

@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%"

Depending on how R installs itself, you could also try to use the where command to retrieve the fully qualified path of the R binary instead of querying values from the registry.

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

Upvotes: 1

Related Questions