从注册表中添加到路径Windows 10年7月8日最新的R安装过程中路径 [英] add latest R installation path from registry to PATH windows 7/8/10

查看:151
本文介绍了从注册表中添加到路径Windows 10年7月8日最新的R安装过程中路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的Windows批处理。

Hi I'm new to windows batch.

我要伸手一runMe.bat文件给同事打电话 RSCRIPT myRfile.R 来处理一些数据文件。但是,我的同事已经出了名的安装 - [R不同的地方,我不能指望他们知道如何RSCRIPT添加到PATH甚至在R $ C $角

I want to hand out a runMe.bat file to co-workers calling Rscript myRfile.R to process some data files. But my co-workers have notoriously installed R various places and I cannot expect them to know how to add Rscript to PATH or even to code in R.

我想.bat文件查找最新安装的R路径,添加[该目录] \\ BIN \\ I386 \\到PATH暂时的。

I would like the .bat file to lookup path of latest installed R and add [that directory]\bin\i386\ to PATH temporarily.

我想象:


  • <一个href=\"http://stackoverflow.com/questions/445167/how-can-i-get-the-value-of-a-registry-key-from-within-a-batch-script\">iterate子文件夹注册表 HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Rcore \\ r \\ 来找到最后和最新的R-版本文件夹

  • iterate the subfolders of registry HKEY_LOCAL_MACHINE\Software\Rcore\R\ to find the last and latest R-version folder

在此注册表子目录获得** ** INSTALLPATH例如的keyValue =C:\\ r \\ R-3.2.2 \\

用\\ BIN \\ I386 \\串联 - &GT; C:\\ r \\ R-3.2.2 \\ BIN \\ I386 \\ - &GT; RPATH

PATH%PATH%; RPATH

我preFER该RPATH不会永久添加到PATH。我的同事可能有相当限制Windows管理员权限反正。

I prefer that the Rpath is not permanently added to PATH. My co-workers probably have quite restricted windows administrator privileges anyway.

非常感谢你!

奖励:我公司主要有32位Windows操作系统的安装,但会在遥远的未来的某个时候升级。我不介意只执行ři386的版本。运行时间和内存REQ。很谦虚。

推荐答案

我觉得像下面会做你想要什么:

I think something like the following will do what you want:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET RKEY=
SET RPATH=
FOR /F "tokens=* skip=2" %%L IN ('reg.exe QUERY HKLM\Software\R-core\R /f * /k ^| sort') DO (
    IF NOT "%%~L"=="" SET "RKEY=%%~L"
)
IF NOT DEFINED RKEY (
    ECHO Unable to qyery registry key HKLM\Software\Rcore\R
    EXIT /B 1
)
FOR /F "tokens=2* skip=2" %%A IN ('REG QUERY %RKEY% /v "installPath"') DO (
    IF NOT "%%~B"=="" SET "RPATH=%%~B"
)
IF NOT DEFINED RPATH (
    ECHO Unable to query registry value %RKEY%\installPath
    EXIT /B 2
)
IF NOT EXIST "%RPATH%" (
    ECHO Found path for R (%RPATH%^) does not exist
    EXIT /B 3
)
IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
    SET "PATH=%RPATH%\bin\x64;%PATH%"
) ELSE (
    SET "PATH=%RPATH%\bin\i386;%PATH%"
)
Rscript myscript.r

首先,我们让本地模式,这样我们设置时该批处理文件退出(即使您使用CALL来调用它)会恢复所有变量。接下来,我们使用取消设置两个变量,所以我们可以测试是否被晚code设置。

First, we enable 'local' mode so all variables we set will revert when the batch file exits (even if you use 'CALL' to invoke it). Next, we unset the two variables used, so we can test whether they are set by later code.

第一个for循环将因此RKEY最终设定为下\\ r最后的关键,并且排序将有望命令他们这样,最新的安装将最后结束每个结果执行一次。内部if语句是只是为了确保空行会被忽略。

The first for loop will execute once per result, so RKEY ends up set to the last key under \R, and sort will hopefully order them such that the newest installation will end up last. The inner if statement is just to make sure blank lines are ignored.

接下来是一个基本的错误检查,以确保r键设置(如果该注册表项不存在,为前)。

Next is a basic error check to ensure that rkey was set (in case the registry key doesn't exist, for ex).

下一个for循环应该只重复一次,并提取只是价值的一部分,从所选键INSTALLPATH值。该为只是用来跳过无关的线条和标记。然后测试是否该值被发现,并且发现值是否实际存在与否。

The next for loop should only iterate once, and extracts just the value part from the installPath value in the selected key. The for is just used to skip irrelevant lines and tokens. Then a test whether the value was found, and whether the found value actually exists or not.

最后,基于该架构更新路径,并运行该脚本。

Finally, update the path based on the architecture, and run the script.

这篇关于从注册表中添加到路径Windows 10年7月8日最新的R安装过程中路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆