Batch file 使用REG查询确定安装路径

Batch file 使用REG查询确定安装路径,batch-file,registry,Batch File,Registry,我正在使用批处理文件通过REG查询确定R的安装路径: 在我将R更新到版本3.4.1之前,它一直工作得很好,该版本将InstallPath的键写入windows注册表中的子文件夹 由于此子文件夹由R版本命名,并且我希望批处理文件独立于R版本工作,因此我希望从任何现有子文件夹获取InstallPath。如何管理该操作?在执行reg命令行工具时,通过指定query/s命令行开关,可以递归查询注册表项及其子项的所有值。以下批处理脚本检索在注册表项HKCU\Software\R-Core\R64或其任何子

我正在使用批处理文件通过REG查询确定R的安装路径:

在我将R更新到版本3.4.1之前,它一直工作得很好,该版本将InstallPath的键写入windows注册表中的子文件夹


由于此子文件夹由R版本命名,并且我希望批处理文件独立于R版本工作,因此我希望从任何现有子文件夹获取InstallPath。如何管理该操作?

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

@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%"
不幸的是,where不起作用,但是您的批处理脚本工作起来很有魅力。
@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%"
for /f "delims=" %%e in ('where r') do set "r=%%~e"