I was wondering if it's possible to execute a bat file i created via SAS?
Reason for this is, unfortunately I do not have admin privileges to schedule the execution of my batch file on task scheduler when I'm not logged in...I need to be logged in for the batch file to execute.
On the contrary my team schedules jobs on SAS so I was wondering if there is a command that can execute my .bat file through the same concept?
I'm happy to accept any other solutions
Thanks.
I always suggest using a pipe fileref when calling OS commands, this way you can capture both STDOUT and STDERR.
data _null_;
infile "C: & C:\path\to\your.bat 2>&1" pipe;
input;
putlog _infile_;
run;
To explain the command:
1) First, change the drive (not necessary if SAS is on the same drive as the .bat file). This is the C: part.
2) Next, execute the file (C:\path\to\your.bat)
3) Finally, redirect STDERR to STDOUT (the 2>&1 part). This will show you any errors.
If you still get nothing, try executing the batch file manually using the same account as you use in SAS, and - on the same machine (eg where the SASApp server is hosted). In a batch environment this may be a batch user. In a Stored Process context it may be sassrv (or equivalent).
options noxwait noxsync;
x 'path\to\your\file.bat';
Related
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.
I have a Python project where I use sqlite3 to save data.
I want to do a backup of the database ( I am really worried about datalock, cause my software will be used by like 10 peoples which will have to access to my database to write or read, and without luck, someone will try to access to the database at the bad moment, even if I will increase timeout)
If I do it manually with the Windows cmd, there is no problem.
v:
cd V:\directory\
sqlite3 mydatabase.db"
.backup backup_db.db
I try to do a batch file which will be call every hours by my python software to do it automatically.
I applied the same commands in it.
The batch file is launched, but the process stop after the opening of the database.
The dot command is not executed.
Where is my mistake?
Batch files do not work the way you think they do.
You currently think that after the line sqlite3 "mydatabase.db" you are in some kind of "database mode" and all the following lines in the batch file are passed to the sqlite3 process.
That's not the case.
Every line in a batch file is executed after the previous like has finished running.
The line sqlite3 "mydatabase.db" starts the sqlite3 process... and then waits until this process exits. Which never happens unless you do it manually by pressing a key.
And after that cmd.exe will try to execute the command .backup backup_db.db, but since that's not a command cmd.exe understands, it will fail with an error ("'.backup' is not recognized as an internal or external command, operable program or batch file.").
What you really want to do is create a script file and pass it to for SQLite for processing. This can be done by
redirecting a file into the sqlite3 process. Assume that create_backup.txt contains the commands to create a backup:
sqlite3 "mydatabase.db" < create_backup.txt
piping the file into the sqlite3 process, e.g. using type:
type create_backup.txt | sqlite3 "mydatabase.db"
alternatively you can use echo to output a command:
echo .backup backup_db.db | sqlite3 "mydatabase.db"
Obviously the echo method is easier for one-liners whereas the input redirect with < or writing out a file with type are easier to for more complex, multi-line operations.
Using the -init parameter of sqlite3.exe is another option you can try. Check the SQLite documentation.
The syntax for the sqlite3 executable is sqlite3 [db_file] [command]. So, in your batch file, you should include .backup on the same line.
v:
cd V:\directory\
sqlite3 mydatabase.db ".backup backup_db.db"
I want to write the script where I need to copy one folder from server on multiple servers (100) and same time When I run this batch file I want log file that on how many servers copy successfully completed with date & time.
I am able to copy the folder from one server to other using xcopy command but need help in capturing results.
Time /T
Will print the current time to the output stream
The easiest thing to do is to just capture all of the output from your batch file execution to a file. To do this, just use a command of the form:
MyBatchfile.Bat > Logfile.txt 2>&1
to pipe both stdout and stderr to a file.
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 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.