How to capture logs in batch file - batch-file

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.

Related

How to start Python virtual environment in .bat batch file?

I need to set up a long list of temporary environment variables every time when I start a Python Flask app on Windows 10. Now I would like to create a batch file to run with all settings in one double click. The following lines run well if I copy them and paste to cmd prompt, but I couldn't run them in the batch file.
The execution of batch file always gets tripped and exited at the 2nd line venv\scripts\activate in the batch file, which has no issue at all if I copy and paste line by line at cmd.
cd C:\py\project
venv\scripts\activate
set env1=val1
set env2=val2
set FLASK_APP=some.py
flask run
One of the many (far too many) quirks of .bat files is that if you launch another .bat file, it doesn't know where to return to.
You need to explicitly call it:
call venv\scripts\activate
you can just use
start venv\Scripts\activate
This will open up a new terminal just like that... you can pass other commands using the && or & sign.

Executing bat file via SAS

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';

Why can't we execute multiple statements of BigQuery in a batch file?

I have observed when we run a simple gsutil or bq statements in windows command prompt they will run successfully
ex 1 : gsutil cp Desktop\MyTraffic.csv gs://BI/
ex 2 : bq load --autodetect --replace --source_format=CSV myDataSet.myTable gs://BI/MyTraffic.csv
but if we run them both in using a batch file like 'runbq.bat' , only first statement will run and the command prompt will terminate immediately without any error
Even adding a pause statement between them won't hold the command prompt window
runbq.bat :
gsutil cp Desktop\MyTraffic.csv gs://BI/
pause
bq load --autodetect --replace --source_format=CSV myDataSet.myTable gs://BI/MyTraffic.csv
As mentioned by Aacini, when running a batch script within a "master" batch script, it is required to use the call command in order to pause the execution of the current batch file and wait until the called batch file completes; in this way the process can return and continue in the original flow. However, when the call command is not added, the original batch file stops and the called batch file starts executing without returning to the original flow.
Another possibility is to use the start command instead in case you don't want to lock the "master" script.

Batch Files - Closing Opened Text Files

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.

Write child batch files output to the log file created for parent batch 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.

Resources