capture cmd output in a batch file - batch-file

i need to capture the output of a batch file (maintenance.bat) that will run on a schedule to restart services and run some maintenance. i need to see the output of the bat file.
my bat file calls other bat files. here is an example
pushd d:\workingdirectory
call servicecontroller.bat services.stop
call rotatelogs.bat
call pushconfigupdates.bat
popd
i need to capture the output of all the commands ran.
Thanks

try with
myfile.bat > log.txt
For more info you can check here.

Related

Batch file when executing from another folder

I am trying to execute a batch file (run.bat) to execute another batch file (clear.bat) located in another folder.
Run.bat looks like this:
call "C:\Documents\BatchFiles\clear.bat"
The clear.bat executes an exe that clears a portion of a .bin file. This batch works when executed independently.
C:\Documents\BatchFiles\clearUtility.exe C:\Documents\BatchFiles\firmware.bin
But when I execute the run.bat batch file the firmware.bin file does not get cleared.
Please help me with this.
I thank you in advance!
I tried adding a few more lines to the clear.bat file to see some output on console.
`C:\Documents\BatchFiles\clearUtility.exe C:\Documents\BatchFiles\firmware.bin
echo DONE!
pause`
I can see the output on the screen when I execute run.bat, but the firmware.bin file still does not get cleared.
Most likely the clearUtility expects the binfile to be in the current working directory.
So change the clear.bat to first set the current directory before executing the clearutility.
#echo off
pushd %~dp0
clearUtility.exe
popd

Execute a copy command using a batch file by opening CMD

I'm creating a batch file on my desktop which has a simple code:
%SystemRoot%\system32\cmd.exe
This will open up Command Prompt.
Then I need to copy paste following code into the command prompt.
for /r "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\Non-Conformance Reports\" %i in (NCR*.pdf) do copy "%~fi" "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\NCR Log Tracking\PDF Destination from DOS\" /Y
The above command simply copies and pastes PDFs from one directory to another directory.
Is there a way to write the entire thing into a batch file?
Desired output is:
A Desktop Icon of a BAT File.
Double clicking on it will do two things: Open up Command Prompt and Executes the Copy command.
Closes the Command Prompt once done
Once that's done, I can simply use Windows Task Scheduler to run this Bat file everyday at 5:00 AM.
All the helps are appreciated. Thank You.
Create a batch file on the desktop and use the following code...
#echo off
for /r "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\Non-Conformance Reports\" %%i in (NCR*.pdf) do (
copy "%%i" "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\NCR Log Tracking\PDF Destination from DOS\" /Y
)
Double Click on the batch file anytime you want to run it.
That's it! =)

Use batch file to insert command in a prompt opened by another batch file

I am using the QGIS software, which includes the OSGeo4W.bat file. This file opens a prompt, rewrite the path variables and include some others, like the python2 enviroment and some sitepackages like the Qt4 installed with QGIS. When opened the .bat file, it opens:
The problem is that i need to insert many commands in here so many times per day, like this one that converts a .ui file made by QtDesigner in .py:
pyuic4 -x C:\Users\Roberto\a.ui -o C:\Users\Roberto\a.py
As this is too much time consuming, i decided to write a batch file, call the OSgeo4W.bat and just add theses commands, but it doesnt work. The commands after the call are not runned. How can i run commands in a batch file inside the prompt created by another batch file? I am using Windows8.1. my batch file
#echo off
call "C:\Program Files\QGIS 2.18\OSGeo4W.bat"
pyuic4 -x C:\Users\Roberto\a.ui -o C:\Users\Roberto\a.py
rem more codes here
pause
You could try the Start command to execute the commands, you could also use timeout to wait before each of the executions.
#echo off
call "C:\Program Files\QGIS 2.18\OSGeo4W.bat"
start pyuic4 -x C:\Users\Roberto\a.ui -o C:\Users\Roberto\a.py
start rem more codes here
//you can use timeout 5 to wait to execute next command
start rem ***
start rem ***
pause

Run multiple batch files from one master batch

I have multiple batch files. Each batch file opens an exe file and run a command. The command compiles the results in csv format. This all works like heaven.
I am trying to create a master batch which could run each of the batch file one by one.
The individual batch file names are as 1.bat, 2.bat . . . .32.bat
I tried,
Call 1.bat
Call 2.bat
.
.
.
Call 32.bat
but it is only running the first command...
I also tried without call, same result.
I am using the following code in each batch file:
Start /D "C:\test 2\" CSC.exe "1.lq"
Start /D "C:\test 2\" CSC.exe "2.lq"
and so on
Can anyone help me crack this?
All I want My master bat to run first batch, on completion run second and so on so forth.
Hope it makes sense.
I really appreciate your help on this guys.
Shei
Try like this:-
START /WAIT batch1.bat
START /WAIT batch2.bat

Closing Batch File

I wrote a batch file, which has 3 .bat file running in background. I have another batch file which has 3 .bat file, which is used to stop those .bat file which ran in first batch file. All this is working fine, but after stopping those .bat files, first batch file's command window is not closing. I gave 'exit' to both the batch file which I wrote.
Please help me in this.
You could try starting the other batch files with
CMD /C
alternatively when they are supposed to close, you could try closing them by name directly from the other batch file:
taskkill /F /IM batchname.bat
Are you using
call batchfile.bat
to run the batchfiles? If not, the flow will be unexpected.
if you could convert it to .exe, with Bat-To-Exe-Converter, you could use:
tskill [program]
Like if you want to close a batch file which is converted called 'helloworld.exe':
tskill helloworld
What you also could use,
is:
tskill cmd
And do this a few times. It will close 1 commdandprompt/time.
I do a delayed close with some of my batch scripts this way:
FOR /l %%a in (30,-1,1) do (TITLE %TITLE% -- Closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)
EXIT /B 0

Resources