Logging the output of a cmd window which is opened by another window - batch-file

I am trying to log the outputs from a cmd window. I am running a bat file which is producing some outputs and I want to log them.
For example I am running the 'test.bat' file from cmd. This opens another cmd window and the output is displayed over there. I want to log the output of the second window.
If I write test.bat > result.log it only logs the terminating message from the 1st cmd window.
Any help or hint will be much appreciated.
These are the contents of the batch file (say test.bat) that I am trying to run from cmd
start CPU.bat
timeout /t 10
taskkill /IM api_test.exe /F
The contents of CPU.bat are
.\api_test.exe cpu -m "name_of_some_model_to_be_tested"

The simple solution is using as first command line:
start "CPU" %ComSpec% /C ""%~dp0CPU.bat" >"%UserProfile%\Desktop\CPU_Output.log" 2>&1"
This command line starts one more Windows command process with option /C to close this command process after execution of the specified command line finished. The console window opened gets the title CPU.
The started Windows command process executes the batch file CPU.bat which is in directory of the batch file containing this command line with full qualified file name. %~dp0 expands to full path of currently executed batch file always ending with a backslash and therefore concatenated with just CPU.bat to full qualified file name of this batch file.
The standard output of CPU.bat is redirected into the file CPU_Output.log in desktop directory of current user. The error output is redirected into the same file.
Please read the Microsoft article about Using command redirection operators for an explanation of > and 2>&1.
Open a command prompt window, run start /? and read the output help about this command. Next run cmd /? and read again the output help, especially the section about how the command line specified after option /C or /K is interpreted by Windows command processor which is very important here.
But CPU.bat is not needed at all on using this command line as first command line:
start "CPU" %ComSpec% /C ""%~dp0api_test.exe" cpu -m "name_of_some_model_to_be_tested" >"%UserProfile%\Desktop\CPU_Output.log" 2>&1"

Related

Batch run commands in child command prompt

I have the following in a batch file that needs to be run via Windows Task Scheduler. This is an example script.
#echo off
echo This is parent command prompt
<some external executable with some arguments> & REM This launches another command prompt with special environment variables set
REM I want the following command to be run in the command prompt that got launched above
echo This is child command prompt
How do I run the last commands mentioned above in the command prompt that got launched?
OR
I launch the child command prompt independently that keeps running. Put the commands to be run in that command prompt in a batch file. Write some more batch code that somehow identifies that child command prompt and executes the commands in that child command prompt. Not sure how to do this either.
Following is a cooked up simple example to show what I mean
#echo off
echo This is parent command prompt
start cmd.exe #cmd /k "echo hello" & REM This launches another command prompt
REM I want the following commands to be run in the command prompt that got launched above
echo This is child command prompt

How run batch script to get output in text file

Below executable return string values in command but when i execute below batch script it pop up different command line console and exits hence i am not getting any values in output.txt
how to capture this result ?
c:\
cd C:\Windows\System32
start usbinvoke.exe argument >c:\result\outpput.txt
pause
usbinvoke.exe argument > C:\result\output.txt
Start starts programs in unusual ways. See start /?
See Command to run a .bat file
Your other commands are unnecessary.
You right click a shortcut to cmd and tick Run As Administrator on the compatibility tab
c:\
cd C:\Windows\System32
usbinvoke.exe argument >c:\result\output.txt
pause
start does not wait unless you use /wait argument. Suggest remove start and just run the executable.
You cannot redirect streams with a process that does not wait as the no handle is attached to the process.
If you require start then use arguments /b (same window) and /w (same as /wait).

move command fails from batch file. But runs successfully when executed from command prompt

I have a simple move command as:
move tfa.war ..\..\..\tomcat\webapps
When i run this command in command prompt it works fine. But when i put this command in a batch file with variables like so, and execute the batch file in command prompt it fails with the message "The system cannot find the file specified".
cmd.exe /C webapp_deploy.bat tfa
And the contents of webapp_deploy.bat:
#ECHO OFF
set app_name=%~1
set webapps_directory=..\..\..\tomcat\webapps
move %app_name%.war %webapps_directory%
Any help would be much appreciated!

How can I use a batch file to write to a cmd file?

i want to write some codes in cmd by batch file
{CreateObject("WScript.Shell").Run "%comspec% /c start /wait cmd.exe", 0, True }
this code opens cmd.exe ..
How can I edit this file with my own dos commands ?
i dont want use it in txt file?
my question is to write some command lines to cmd.exe
not to txt file by "echo > & echo >> "
i need to open cmd and start write instructions with a batch file
and how i get some information from the cmd and place it in another script to use it later
Wscript.shell.run uses shell32 to start files. You ask it to specifically start cmd.exe (%comspec%), which you ask that cmd.exe to ask shell32 (the start command) to start cmd.exe. Put the commands you want to run in a batch file (that's a text file).
Wscript.shell.run "c:\folder\batchfile.bat"
or if you prefer
Wscript.shell.run "cmd /c c:\folder\batchfile.bat"
or if only one command
Wscript.shell.run "cmd /k dir"
see
cmd /?
I wanted to start cmd, then start write code which was saved in batch file
The code will be like this:
{ START cmd.exe /k "netsh wlan show profiles" }
This code will open cmd and show you the saved profiles

Script started minimized saves empty file

I need to run script minimized and save it's output. I used following line but saved file is empty:
start /MIN script.bat > file.txt
How to start script minimized and save it's output to file?
You are outputting the results of start to file.txt. The file is empty because start /MIN doesn't produce any output in the current console. What you want is to have the file redirection as part of the started command. Adding quotes fixes this.
Also, when I tried this, it created a new minimized command prompt which ran script.bat and then waited for further commands. I assume this probably wasn't your intent. To make the new console close after script.bat exits, pass the /c parameter to cmd.exe
start /MIN %comspec% /c "script.bat > file.txt"

Resources