How do I run a .bat file without a terminal (remaining open)? - jar

I want to run a jar file with a .bat (the jar file doesn't seem to want to open on its own but thats a different issue for now) but as the java file runs for a long time, the command prompt remains open (while the .bat/.jar is still running)
I do not want this.
I read somewhere that you can use a .cmd file and the command(s):
cmd /c bat.bat
exit
To run a bat file without a command prompt. But that isn't working for me. When I click the .cmd program it just opens a command promopt and keeps printing "cmd /c bat.bat exit" over and over in a loop.
What am I doing wrong, was my .cmd command wrong? Is there another way to run a .bat without a command prompt remaining open?
Thanks alot.

From here:
Save the following as wscript, for instance, hidecmd.vbs after
replacing "testing.bat" with your batch file's name.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false
The reference is here
http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx

Related

How to "source" (not run line by line) an R script from the Windows command line

I am creating an R script myscript.R which manipulates an excel file by means of XLConnectpackage.
The point is it refers to several external files: the excel file itself and another R file (functions library), so I need to set a working directory in the location of the script (so that relative paths to external files work properly).
I am using the following in my script
new_wd <- dirname(sys.frame(1)$ofile)
setwd(new_wd)
When I source the script from my RStudio it gets the job done. The problem is that the script is to be used by non-programmers, non-Rstutio-users, so I create .bat file (which I want to turn into an .exe one)
"C:\Program Files\R\R-4.0.3\bin\Rscript.exe" "C:\my\path\to\myscript.R"
It executes the script line by line but sys.frame(1) only works when sourcing.
How could I solve it?
Thanx
I have found a solution and it works properly.
From CMD command line or from a .bat file one can add an argument -e to the command, so that you can use a R language.
absolute\path\to\Rscript.exe -e "source('"relative\path\to\myscript.R"')"
It worked for me.
Besides, as Compo commented, I think there's no need for a .exe file, since a .bat does the job.

Use bat file directory in the commands

I would like to use the directory of my bat file to run my R script.
My R script is in the same directory as my bat file, I tried:
"C:\Program Files\R\R-3.1.2\bin\x64\RScript.exe"
"%CD%\script_to_run.R"
but the cmd immediately closes.
This works if I specify the entire path of my script instead of using %CD%.
Can I have some help, please?
The simplest fix is to use:
"C:\Program Files\R\R-3.1.2\bin\x64\RScript.exe" "%~dp0script_to_run.R"
Where %0 references the running batch script and %~dp0 references the drive and path of the running batch script, (ending with a trailing back slash).
To start, or run a program in a batch script, you have to right start at the beginning. It would look like this,
start yourfilepath
I hope this helps, if not, tell me and I will try to help.

Why .bat file doesn't initiate R file

I would like to invoke R script by using a .bat file.
For testing, I saved an R file on the desktop in the name print_test.R
The code in this file is:
p<-500
write.csv(p,"print.csv")
I wrote the .bat file :
#echo off
R CMD BATCH C:\Users\User\Desktop\print_test.R
UPDATE:
After adding pause to the .bat file I got the following text:
"'R' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . ."
When I click on the bat file (also saved on the desktop) there is a blink of the CMD window but the print.csv file is not created.What is wrong?
Consider using the automated utility, Rscript.exe, as the R CMD BATCH might be considered a legacy command. See post: R.exe, Rcmd.exe, Rscript.exe and Rterm.exe: what's the difference?
Rscript as an environment PATH variable:
#echo off
Rscript "C:\Users\User\Desktop\print_test.R"
Rscript not as a environment PATH variable (where you specify full path of the executable -usually in bin folder of installation):
#echo off
"C:\Path\To\Rscript" "C:\Users\User\Desktop\print_test.R"
OR if paths do not have spaces:
#echo off
C:\Path\To\Rscript C:\Users\User\Desktop\print_test.R
I followed Sumedh's tip :
here is the new code:
#echo off
"C:\Program Files\R\R-3.2.4\bin\x64\r.exe" CMD BATCH "C:\Users\User\Desktop\print_test.R"

Execute batch file from Matlab

I have a Matlab function that finds the path where this function is within my pc and then runs a bat file on that same directory. This bat file is meant to execute an R script but for a strange reason is failing to do so.
This is my Matlab function:
function [] = myFunction(arg)
% Find the directory of the executing script
thisDir = fileparts(mfilename('fullpath'));
% Save arg as a csv on this directory, this will be read by my R script
tmpDir = strcat(thisDir,'/tmp.csv');
csvwrite(tmpDir,arg);
% Specify the command to run
dosCommand = ['call "' thisDir '/runRscript.bat"'];
dos(dosCommand);
end
The bat file has the following code:
"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH runRScipt.R
When I run the function in Matlab I get the below message:
C:\Users\...mypath...>"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH
runRscript.R
Since I get this message in Matlab I have no doubt it is finding and reading the batch file, but it fails to execute the R script. I know the bat file works as expected since I can run it through the command line (with the command that should be the "dosCommand" on the Matlab script) or by clicking twice on the .bat file.
I found the answer. For a strange reason the dos() command would not work, but the system() command will do the job. Then the code will look like this:
function [] = myFunction(arg)
% Find the directory of the executing script
thisDir = fileparts(mfilename('fullpath'));
% Save arg as a csv on this directory, this will be read by my R script
tmpDir = strcat(thisDir,'/tmp.csv');
csvwrite(tmpDir,arg);
% Specify the command to run
sysCommand = ['call "' thisDir '/runRscript.bat"'];
system(sysCommand);
end
And the batch file:
"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH runRScipt.R
Instead of R.exe please try Rscript.exe. R.exe runs R code in interactive mdoe while Rscript runs the code in batch mode. Ideally you should find Rscript executable in the same path as in R executable (i.e. "C:\Program Files\R\R-3.2.2\bin\x64" in your case)

Schedule to run a executable jar file on windows 7

I created a task in task scheduler on Windows 7 system and made it repeatable every 10 minutes.
In program, i selected the executable java jar file. But it does not run the jar file at the scheduled time.
When i double click and run the jar file, it runs as desired.
The Jar just pops up a dialog box.
Any inputs as to where i am going wrong is appreciated.
Firstly make sure Java is set in the enviroment PATH by opening cmd.exe and typing java -version. If you get back the java version, then you are fine. (If not see Update the PATH Environment Variable (Microsoft Windows))
Then create a text file, save it as run.bat and type inside:
java -jar <insertjavajarnamehere>.jar
Make sure the bat is in the same directory as your jar file.
Now go in Windows Task Scheduler > Create Basic Task > ... >Start a program > and browse for your .bat file.
Also, set Start in to the path where your .bat and .jar are located. Create your task and it should run fine afterwards.
Edit: To avoid the shell being visible a simple trick is to create a VBS file
Create a run-invisible.vbs, and type:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("C:\Users\pathtobat\run.bat"), 0, True
And schedule that instead of the bat (make sure you update the path to your bat in the vbs file)

Resources