We help IT Professionals succeed at work.

Batch File not running in Windows 7

Medium Priority
968 Views
Last Modified: 2012-05-12
I have a very simple batch file as shown below that is used to run a script in R (similar to Matlab).

This script always worked on Windows XP, and works if I paste the text into the Windows 7 "Run" command prompt.  It does not run if I double click the batch file.

Does anyone know why this might be?  My ultimate goal is to set a scheduled event to run the batch file.
R CMD BATCH "C:\Code.r" "C:\output.r"

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
The path to "r.exe" probably isn't set on this machine.
Simplest and safest solution is to specify the full path to the program, for example like this:
"C:\Program Files\Whatever\R.exe" CMD BATCH "C:\Code.r" "C:\output.r"

Open in new window

Author

Commented:
how would I specify the path?  I have added the old Run menu option to the Windows button (bottom left) and if I simply type R <enter> the program will load.  However, the same does not occur if I type R in the search prompt...So I guess for Windows 7 you have to do something else.

Author

Commented:
Just checked the Path System Variable and I have this entry:

C:\Program Files\R\R-2.13.2\bin;

which is the correct path.

Commented:
Sounds like a security issue, try this:

http://www.vistaclues.com/run-a-batch-file-as-an-administrator/

If this works you could also try turning off UAC on the Win 7 machine if you don't want to do as the link suggest.

Author

Commented:
Running as administrator did not work, I'm afraid.

A prompt flashes up very quickly (i had to print screen to capture it) saying:

'R' is not recognized as an internal or external command,  operable program or batch file.

Note, this comes up whether I run as administrator or not.
error-message.png

Author

Commented:
That said, when I use the full path in the batch file, it did work (below).

Just wondering why I can start R from the Run command prompt, yet using R in a batch file does not work.


"C:\Program Files\R\R-2.13.2\bin\R.exe" CMD BATCH "C:\Code.r" "C:\output.r"

Open in new window

Commented:
Ahh...because that is a windows keyboard shortcut.

When running a batch file you can not use the R command unless you have it full path as well...
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
It works from the Run field because the program probably created an entry in "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths" - you'll probably find a key "r.exe" with a default entry containing the complete path.
But if the path is specified as well in the PATH environment variable, it should work as well. But the safest solution is to use the full path in the script; that way, even if another tool named "r.exe" should end up in the system32 folder, the script will still start the correct program.
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
In your batch file put:

@echo off
echo %cd%
echo %path%
r.exe
pause
exit /b

To see what is going on and what it thinks your path is.

Author

Commented:
@dragon-it

This is what I get:

C:\VP\VP - Analytics\vModels\WorldIndices
C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;
C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Program File
s\Dell\Dell Data Protection\Access\Advanced\Wave\Gemalto\Access Client\v5\;C:\Pr
ogram Files\Windows Live\Shared;C:\Program Files\Common Files\Roxio Shared\DLLSh
ared\;C:\Program Files\Common Files\Roxio Shared\OEM\DLLShared\;C:\Program Files
\Common Files\Roxio Shared\OEM\DLLShared\;C:\Program Files\Common Files\Roxio Sh
ared\OEM\12.0\DLLShared\;C:\Program Files\Roxio\OEM\AudioCore\;C:\blp\API;C:\blp
\API\dde; C:\Program Files\R\R-2.13.2\bin;C:\Program Files\QuickTime\QTSystem\
'r.exe' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
ok, and where is r.exe? In C:\Program Files\R\R-2.13.2\bin ?

start | run on windows 7 looks anwhere on the drive, not just the path.

Might be worth moving ther path entry for R closer to the top of the path after the windows dir ones.  

Author

Commented:
yes, R.exe is in the \bin\ folder

Sounds to me like Path does not work reliably with Windows 7/command prompts.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
There is a space in front of "C:\Program Files\R\R-2.13.2\bin"; remove it, and it should work (in a freshly opened command prompt).
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Well spotted there, wasn't clear with the proportional font.  That will certainly cause it as it won't search into that dir from cmd.exe prompt using %path% but would using start | run as that searches all.

Seriously though I would suggest running this as the actual path...

1. A scheduled task user could have a different path list
2. Any program that drops an r.exe anywhere else further up the path would get itself executed

etc. so should really do either as batch

@echo off
cd /d C:\Program Files\R\R-2.13.2\bin
R CMD BATCH "C:\Code.r" "C:\output.r"

or single command line with path as suggested.

To change the path if you don't know look in
Control Panel
System
Advanced system settings
Environment variables
Find path, Edit it ...

Steve
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Hmm, typed that 1/2 hour ago but hadn't submitted... I did suggest looking at the path but was beaten to spotting the space..
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
dragon-it,
the Run box doesn't search the whole disk; but except for the regular path, it searches as well in "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths".
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
But on Win7 it also presents other files, i.e if you start to type in r ... it will show suggestion list of all files beginning with r, exe's, docs, videos, docs, etc.

Steve
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
i.e. it is really a search box, not run.  Do you know how you can get a simple "run" box back btw, I use search of course, and have a script which prompts for a command but the run box worked better than search IMO.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Properties of the "Start" button, tab "Start Menu", button "Customize ...", checkbox "Run command".
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Thanks for the freebie answer oBdA ... thought I knew Win7 pretty good but didn't know of that customise button :)
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Just for curiosity, do the following at a DOS command prompt, and let's see what it says.

WHERE r.exe

~bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Whoops, sorry, hadn't refreshed in a while and forgot I hadn't.

~bp