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"
Related
I have a command-line programm that returns values. Like example this simple batch-file:
echo "Hello World"
In a normal situation I can pipe the output to a file like this:
$ my_batch.bat > file.log
The Problem occures if I want to start this in a new console with a specific title:
$ start "NewConsole" cmd.exe /c my_batch.bat > file.log
In this case my batch seems to run, but my file.log will be empty.
Like #Compo and #aschipfl said, the > in your script right now is piping the output of the whole start command (which is nothing) and piping your current script. However, if you either 1. break the > out with ^, the > will pass to the next command prompt, or if you 2. put " around the part of your start command that passes stuff to do to the next command prompt so it passes the things you want to pass including the > pipe.
Overall you could either use this:
start "NewConsole" cmd.exe /c my_batch.bat ^> file.log
or this:
start "NewConsole" cmd.exe /c "my_batch.bat > file.log"
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"
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).
I have a bat file like this:
ipconfig
That will print out the IP info to the screen, but before the user can read that info CMD closes itself.
I believe that CMD assumes the script has finished, so it closes.
How do I keep CMD open after the script is finished?
Put pause at the end of your .BAT file.
Depending on how you are running the command, you can put /k after cmd to keep the window open.
cmd /k my_script.bat
Simply adding cmd /k to the end of your batch file will work too. Credit to Luigi D'Amico who posted about this in the comments below.
Just add #pause at the end.
Example:
#echo off
ipconfig
#pause
Or you can also use:
cmd /k ipconfig
When the .bat file is started not from within the command line (e.g. double-clicking).
echo The echoed text
#pause
echo The echoed text
pause
echo The echoed text
cmd /k
echo The echoed text & pause
Adding pause in (Windows 7) to the end did not work for me
but adding the cmd /k in front of my command did work.
Example :
cmd /k gradlew cleanEclipse
start cmd /k did the magic for me. I actually used it for preparing cordova phonegap app it runs the command, shows the result and waits for the user to close it. Below is the simple example
start cmd /k echo Hello, World!
What I did use in my case
start cmd /k cordova prepare
Update
You could even have a title for this by using
start "My Title" echo Hello, World!
If you are starting the script within the command line, then add exit /b to keep CMD opened
In Windows add '& Pause' to the end of your command in the file.
I was also confused as to why we're adding a cmd at the beginning and I was wondering if I had to open the command prompt first.
What you need to do is type the full command along with cmd /k. For example assume your batch file name is "my_command.bat" which runs the command javac my_code.java then the code in your batch file should be:
cmd /k javac my_code.java
So basically there is no need to open command prompt at the current folder and type the above command but you can save this code directly in your batch file and execute it directly.
javac -d C:\xxx\lib\ -classpath C:\xxx\lib\ *.java
cmd cd C:\xxx\yourbat.bat
the second command make your cmd window not be closed.
The important thing is you still able to input new command
As a sidenote this also works when running a command directly from the search bar in windows.
e.g. directly running ipconfig will directly close the cmd window after the command has exited.
Using cmd \k <command> won't - which was what i was trying to do when i found this answer.
It has the added advantage of always recognizing the command you're trying to run. E.g. running echo hello world from the searchbar won't work because that is not a command, however cmd \k echo hello world works just fine.
I'm trying to save the output of an exe file executed within a batch file to a text file.
I've tried the the following methods but they don't work
This doesn't work since I'm back at the command prompt while the application runs and the text file created is blank.
C:\>myexec.exe > mytext.txt
C:\>_
C:\>Status: Passed
These also doesn't work. I get an empty text file and no output.
C:\>start /wait myexec.exe > mytext.txt
C:\>call start /wait myexec.exe > mytext.txt
This gives me an output at least:
C:\>start /wait myexec.exe
Status Passed
Use /B operator.
This will cause the output to be redirected
start /B /wait myexec.exe > mytext.txt
I had the same issue and solved it like this:
bat file content
START /wait cmd /c "F:\MyInstaller\installer.exe > log.txt"
echo This will get logged after the previous is completed > log1.txt
The & redirection operator duplicates output or input from one specified handle to another specified handle. For example, to send dir output to File.txt and send the error output to File.txt, type:
dir>c:\file.txt 2>&1
Try Application.exe >& file.txt. This will output both, standard output and error stream, to your file.