1

I'm trying to write a small script to find the absolute path of Rscript in a machine running Windows OS. I assume that Rscript must be found under C:\Program Files\R\R-x.x.x directory, where x.x.x represents any version of R. I have written the following script based on several threads (Find file and return full path using a batch file).

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /d %%a in ("C:\Program Files\R\") do (
  if "%%~nxa"=="Rscript.exe" (
    set p=%%~dpnxa
    )
)

Unfortunately, the if condition assigns p to every file and folder in C:\Program Files\R\R-x.x.x. If anyone has any idea of how to find and assign p properly, I would really appreciate it!

Regards,

Juan

EDIT

I'm adding my full script

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

for /R %%a in ("C:\Program Files\R\R-*") do (
  if /i "%%~nxa"=="Rscript.exe" (
    set "p=%%~fa"
    )
)
if defined p (
echo %p%
) else (
echo File not found
)

%p% runShinyApp.R 1> ShinyApp.log 2>&1

1 Answers1

1

Not sure why you want to do %%~dpnxa when you can just do %%~fa anyway, here are some ways.

@echo off
for %%a in ("C:\Program Files\R\*") do (
  if /i "%%~nxa"=="Rscript.exe" (
    set "p=%%~fa"
    )
)

another method is to use where, without having to do an if statement, simply because you specify the executable in the where search. You can recursively search for the path in an entire drive, or specify a path, here for instance we recursively search in "C:\Program Files".

@echo off
for /f "delims=" %%a in ('where /r "%programfiles%" "rscript.exe"2^>nul') do set "p=%%~fa"

EDIT after your comments, this should work.

@echo off
for /f "delims=" %%a in ('where /r "%programfiles%\R\R-3.6.3\bin\x64" "rscript.exe"2^>nul') do "%%~fa" runShinyApp.R 1> ShinyApp.log

I would however recommend you specify the path to runShinyApp.R in the do line.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Thank you for your comments. However, I cannot find any of the *Rscript.exe* files available in my *R-x.x.x* folder (I have 3 different files under *R-x.x.x\bin*, *R-x.x.x\bin\x64* and *R-x.x.x\bin\x386* directories. – Juanma Garcia Jul 28 '20 at 08:51
  • then you must find it. open `cmd` and run `where /R c:\ "rscript.exe"` and show me the result. – Gerhard Jul 28 '20 at 08:59
  • I get what I was expecting `c:\Program Files\R\R-3.6.3\bin\Rscript.exe c:\Program Files\R\R-3.6.3\bin\i386\Rscript.exe c:\Program Files\R\R-3.6.3\bin\x64\Rscript.exe` – Juanma Garcia Jul 28 '20 at 09:04
  • ok, good, so which one of those would you like to use to execute? `x64`, `i386` or directly in `bin`? I will then amend my answer – Gerhard Jul 28 '20 at 09:07
  • The one in `x64` – Juanma Garcia Jul 28 '20 at 09:10
  • See bottom script in my answer, copy as is please. – Gerhard Jul 28 '20 at 09:16