I have a program flow where a database command button writes a file with data from the current record, then executes a batch file (windows) to knit a markdown file using the output from the database as inputs.
"C:\Program Files\R\R-4.1.1\bin\i386\Rscript.exe" -e "library('knitr'); rmarkdown::render('MyMarkdownFile.Rmd', output_file='MyOutput.html')"
The final step is that this file is opened in a browser.
start "" "MyOutput.html"
I do not use a unique file name, the same html file (MyOutput.html in the example above) is over-written each time. Sometimes the markdown process throws an error and halts execution during the knit. In these cases the previous version of the html file is then opened by the next batch command and, to the users, this may be confusing: they may assume they are seeing the current report when in fact they are not. (Note there are clear labels to distinguish, but still ...). I am wondering if there is a way to somehow "know" within the batch file that there has been an error in the knit process and thereby halt execution of the batch so that the html is not opened in the final step.
See Mofi's comment. This is the answer. Simply inserting && between batch commands halts execution when the knit returns an error. Thank you.
Related
My code in the .bat looks like this.
"C:\users\reshade injector.exe"
C:\pathtogame\game.exe "added argument"
"c:\users\reshade injector.exe" <- if I open the actual file, it opens a command window and waits for my other application to launch. Is this a console app?
C:\pathtogame\game.exe "added argument" <- this works and launches my game
Another issue is that when launching the first .exe, a command window stays open. How do I make the command window not appear?
Can anyone help me with these two problems?
*edit for clarity
by opening the actual file, I mean double-clicking on the original reshade injector.exe.
When opening reshade injector.exe, two cmd windows open. They close as soon as I launch the game.exe.
I can't seem to get the reshade injector.exe to launch through the .bat file.
Batch will run any application it finds. If the application does not terminate (eg. it's waiting for user-input) then it will progress to the next instruction.
Some applications work by launching another (the user-interface) and then terminating, leaving the second application resident.
There's no way to tell without observing which approach any random application may take.
So - your batch should execute a file named c:\users\reshade injector.exe.
You say that "fails". Is there an error message generated? You would likely need to run the batch from the "command prompt" to see that message. Note that the Space between reshade and injector must be in the actual filename. If injector.exe is a file in a subdirectory called reshade, then you need a backslash between reshade and injector.
Having resolved the actual name of the first executable, try
#echo off
setlocal
start "" "firstexecutablename"
start "" "secondexecutablename" "arguments"
The first two lines prevent "command echoing" which is used for debugging and establish a "local environment" which ensures the "environment" for the batch file remains unchanged when the batch terminates. In the current case, these two lines are probably unnecessary, but it's SOP for batch files.
The batch should then close its session and window once it has attempted to launch the two applications.
The "" following start becomes the window-title. It can be any quoted string you like, but should not be omitted as start uses the first quoted string it finds as a window-title and may not recognise it as part of the executable/arguments.
I currently have a Windows batch file that runs an .exe file that uses a text file. I am trying to have the Windows batch file run the .exe file multiple times. This, however, requires the use of the same text files to read from. The command prompt gives me the error that the ".txt could not be opened" (I assume from this that it is already open.)
I am trying to see if there is a way in a .bat file to system call to kill that specific text file. The suggestions I see online are to use 'taskkill notepad.exe', but that returns "invalid argument" because the program doesn't open Notepad to use the text file.
Any suggestions would be appreciated.
It sounds like your existing script fails because the first instance of the exe is still open when the second instance starts.
One thing worth trying (and this depends on the nature of the application you are invoking) is to start the executable using the START /WAIT /B ... command. This makes the command interpreter wait for the program to exit before it moves onto the next command, so as long as nothing else is locking the text files you should be OK to move onto the next command.
I have created a batch file with 2000 lines of code and which will peroform various operations in my daily work. For this batch file I have to give some inputs to enable/disable some of the operations. Finally the batch file is ready and working fine.
But I want to have a log file in which everything which is coming on the command window should be logged, including the inputs that I have given to the batch file and also the execution process.
Is there any way do like this? Any one of you can help me?
Thanks in advance...
Nagaraju
You are not telling how you are invoking the batch file. You could try adding redirection to the command line (batchfile.bat > filename.log), although that won't catch whatever you type in as input. You could, however, change the script to print out the input parameters so that they could be caught in the log file.
I can find plenty of answers on the internet about how to open a specific file, e.g. http://answers.yahoo.com/question/index?qid=20080102230630AAfu5dF
However, I need to provide a way of opening a non-specific file in a program called by a batch file.
To explain, here is an example. The user has a folder with 100 files in with the .xyz extension. He wants to be able to double click on ANY file and open it in his "XYZ Viewer," but to run his XYZ Viewer he needs to run a batch file that alters his registry and then runs the actual XYZ Viewer .exe.
If you select the batch file to be the default program via the "Always use the selected program to open this kind of file" tickbox, it will open the program, but without using the standard Windows function of opening the file that instigating the running of the program.
Is there a way to run the program through the batch file and for it to both run the program and open whichever file it was that instigated the running of the program?
I suspect this is impossible, but any suggestions would be very gratefully received!
Cheers.
Edit:
The program does eventually support opening a file placed as an argument to it.
My code is
reg import c:\regent\31.2.03.reg
start C:\Program\Program.exe
Does the program eventually support opening a file placed as an argument to it? In the example you linked, mspaint opens the first parameter given to it.
If your batch file isn't currently doing this, you will have to edit it to contain the batch parameter(s).
See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true for some fuller documentation on it.
Essentially you want to add %1 somewhere like:
reg import c:\regent\31.2.03.reg
start C:\Program\Program.exe %1
i have a batch file, b1.bat which internally starts another two batch files, b2.bat and b3.bat and b2.bat internall calls b4.bat and root batch file, b1.bat,waits until those three(b2,b3 and b4) finishes. In summary, scenario like this:
b1.bat -> b2.bat -> b4.bat
-> b3.bat
I want to write output of all 4 batch files(b1.bat, b2.bat, b3.bat and b4.bat) into single log file, my_log.txt. I want to do this with minimal effort ie., changing less no. of batch files as i have lot of batch files like this without logging. So i want to provide logging for them.
I) Is it possible to control the log file output from parent batch file ie.,b1.bat?
II) Do i need to change all batch files with redirection operator which writes the output to log file?
I could'nt find proper solution for this. Please suggest me in this regard.
Assuming you are NOT doing any asynchronous processing using START, you should be able to simply use:
b1.bat >my_log.txt
You might also want to capture error messages by appending 2>&1 to the command.