Home > other >  Obtain the value data of a Windows Registry
Obtain the value data of a Windows Registry

Time:10-14

I have been writing a Batch Script in Windows 10, that wants to execute sequentially two R Scripts, one using a 32bit R and another using a 64bit R. In order to do this in any computer I share this batch script with, I need to determine the location of the existing R32bit and R64bit installations in the computer that uses this batch script.

In R identifying the folder paths from the Registry can be done with:

# find the path to a R32bit and 64bit R installations 
fp32 <- file.path("SOFTWARE", "R-core", "R32", fsep="\\") 
fp64 <- file.path("SOFTWARE", "R-core", "R64", fsep="\\")

# build the Rscript paths
32bit_Rscript <- paste0(readRegistry(fp32, "HLM")$InstallPath,"\\bin\\i386\\Rscript.exe")
64bit_Rscript <- paste0(readRegistry(fp64, "HLM")$InstallPath,"\\bin\\x64\\Rscript.exe")

However, I have not found a way to do this through an Windows Batch Script. I am able to retrieve the key value:

 reg query HKLM\SOFTWARE\R-core\R32 /v InstallPath
 reg query HKLM\SOFTWARE\R-core\R64 /v InstallPath

However, this query returns a number of values, including the folder path I want, for example:

 HKEY_LOCAL_MACHINE\SOFTWARE\R-core\R64
         InstallPath    REG_SZ    C:\Program Files\R\R-3.6.1

Question: How can I single out from this registry query only the folder path to assign this to a variable in a batch script?

CodePudding user response:

for lets you run a command and get its output as lines you can parse.

@echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION

set v=
for /F "tokens=2,*" %%a in ('2^>nul reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "CurrentVersion"^|find "REG_"') do @set v=%%b
if defined v echo.The value is %v%

(The value queried was changed so it works for everyone running this example.)

  • Related