cmd new window store output - batch-file

I want to open a new command prompt, run an program and save the output (which is right now displayed in the command prompt) in a logfile.
I used this command so far:
cmd /c start "window title" "C:\Program Files\app.exe"
normally I can use
"C:\Program Files\app.exe" >out.txt
to save what is written on the command prompt in a file.
With the need, that the programm is executed in another window, I'm struggling to set the output correctly.
Why do I need the extra window?
--> The program will be called several times. I need a license for that, I'm limited there. If the program is called in the same window, after 3 three times an error occurrs, telling me, that I use to many license at the same time.
With extra opening and closing windows this "license problem" is solved.
But the I cannot find the solution for the output then.
Lua is tagged, since this command is embedded in Lua's os.execute()

You should examine start /? for the full syntax of the command. You need the following elements:
the start command
"window title" (can be "" if you don't wish to specify)
/d "working directory"
"command name"
"command arguments"
... where each token after start is quoted. Example:
start "" /d "C:\Program Files\appdir" "app.exe" ">%userprofile%\Desktop\out.txt"
Use the start command's expected argument structure to pass the output redirection as an argument. Pass it quoted so the cmd interpreter knows you want the output of app.exe redirected, as opposed to the output of the start command (which doesn't natively provide any useful output data).

Windows command line has wicked rules about quoting :-)
This code works as you need:
os.execute([["start "window title" cmd /C ""C:\Program Files\app.exe" > "C:\my logs\log.txt"""]])

I suppose you are looking for something like,
start "window title" "C:\Program Files\app.exe > out.txt"
The redirection is inside the quotes, else it would fetch the output of the start command itself (that output is empty).

Related

Batch - redirect program output to current console

Lets say this is test.bat
START cmd /c "D:\myprogram.exe" arg0 arg1 ^>out.txt
pause
This works fine, but I want to show the output of myprogram.exe inside the test.bat console, instead of creating a text file for it. The problem is that myprogram.exe creates a new console, finishes and closes the console window before I can see the output and so far I can only write it to a text file. How can I redirect the output to test.bat's console window instead?
According to the comments, it appears that the following is all I needed:
#"D:\myprogram.exe" arg0 arg1
From help
C:\WINDOWS\system32>start /?
Starts a separate window to run a specified program or command.
…
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
parameters These are the parameters passed to the command/program.
So to recap. Start is for starting programs in unusual ways.

Start a batch file and close Cmd once it is finished

I have my application spread over multiple directories, each containing a part (e.g. web frontend, mobile application, administration, middleware, backend, ...).
In each of the directories I have one part, and a file compile.cmd which compiles that part and looks roughly like this:
#ECHO OFF
compiler prepare thisPart
compiler compile thisPart
copy resultingFile1 ../bundleDirectory
...
copy resultingFileN ../bundleDirectory
pause
The "pause" is so I can check the compiler output, whether compile failed and which error messages occurred, and then close the window with a single keystroke (mostly, space key).
I now want to have a batch file that calls all these batch files for different application parts in parallel, so I guess I have to open a new shell window for each.
So I wrote CompileAllParts.cmd like this:
cd part1
start "Compile part 1" compile.cmd
cd ../part2
start "Compile part 2" compile.cmd
cd ../part3
start "Compile part 3" compile.cmd
Positive is that I can influence the window title, but the drawback is that new Cmds are spawned which do not automatically close.
This is also part of the documentation of start:
If command is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run.
Is there a hidden parameter to explicitly disable this behaviour?
combine start with cmd. First to set title and working folder, second to use /C and execute the command:
start "Compile part 1" /d "part1" cmd /c "d:\proper folder\compile.cmd"

batch file with relative path and close command prompt

I try this code :
start /d "D:\test\CONTOH\DATA\QGIS2\bin\" qgis.bat
based on :
Bat file to run a .exe at the command prompt
But, I want to be relative path, something like this :
start /d %~dp0\DATA\QGIS2\bin\qgis.bat
based on :
relative path in BAT script
but, nothing happen. So, can someone give me information, what is wrong?
Open a command prompt window and run start /?. This outputs the help for the command START which should be read on using this command to get knowledge about its options.
Run in command prompt window call /? and read the output help pages to understand %~dp0. The drive and path of the batch file (argument 0) always ends with a backslash. Therefore don't add an extra backslash on concatenating it with another string.
And the first double quoted string is interpreted by START as title for the new console window displayed in title bar of the new window. Therefore better specify always a title string which can be also an empty string like "" in case of starting a GUI application on which no console window is opened at all.
start "Running QGIS2" /D "%~dp0DATA\QGIS2\bin" qgis.bat
Also possible is using this command line in batch file:
start "Running QGIS2" /D"%~dp0DATA\QGIS2\bin" qgis.bat
Here start in directory as defined with /D"%~dp0DATA\QGIS2\bin" is 100% correct specified according to help of command START as one parameter string.
But Windows command interpreter accepts also the first variant with just option /D without any folder path and next parameter string "%~dp0DATA\QGIS2\bin" after a separating space is the folder path for start in directory.
The first variant with just /D as one parameter string and "%~dp0DATA\QGIS2\bin" as one more parameter string is easier to read in comparison to second variant with /D"%~dp0DATA\QGIS2\bin" being just one parameter string.

Windows Batch: Executing command with FOR /F -- "command not found"

I have a problem with executing an other command within a FOR-command on Windows 7 Ultimate.
The for-command is part of a batch-file and should parse the outputs of another command.
Usually this:
for /f %%a IN ('tasklist') DO echo %%a
should execute the command "Tasklist" and output its results via echo.
But I always get "command not found".
I tried to execute the command outside of the forloop and it works.
I also tried do execute lots of outer commands within the for, but every command said "command not found".
I also tried the examples of this post:
Batch: Execute command with quotes in for loop with piping to find
I`m sure
that the commands exists
that I have read and execute rights to it
that my User is in Administrator Group
that I run the commands with "Run as Administrator"
But nothing is working in this pc.
The same commands work on another PC wich is also running a windows 7 ultimate.
So has anybody an idea would could be wrong on the pc where all commands are not found ?
Here is an example of my console outputs when I try it with the command "ls".
ls.exe is a executable file from the gun4win project, and its located in the same folder where my batch-file is running.
The windows is in german, so the error output is also in german.
C:\test>test_for.bat
C:\test>rem --- test a command stand-alone ---
C:\test>ls
ls.exe test_for.bat
C:\test>rem --- test same command in a FOR-Loop ---
C:\test>for /F "delims=" %a in ('ls') do echo FOR-OUTPUT: %a
Der Befehl "ls" ist entweder falsch geschrieben oder konnte nicht gefunden
werden.
FINAL EDIT:
The problem was as wrong value for the system envoirement variable ComSpec.
I changed ComSpec in Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen" to "C:\Windows\system32\cmd.exe and the problem was solved.
Thanks to #foxidrive and #jeb
If you use FOR /F and get the error 'something' is not recognized as an internal or external command, operable program or batch file for every program, even internal commands, the most common cause is a wrong ComSpec variable.
You can check the variable with set ComSpec, it should be C:\Windows\System32\cmd.exe.
It will not help to change the variable on the command line, the cause is described at DosTips: ComSpec strange behaviour
If the variable contains a different value, you should correct this under
For a German system:
Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen
For an English system:
Win Key+Pause Key->Advanced System Settings->Environment Variables->System Variables
There exists a second possible cause for strange FOR /F behaviour
If the AutoRun feature can be enabled in the registry ...\Command Processor\AutoRun, for more details see cmd /?.
The AutoRun feature can start a batch file each time a new cmd.exe instance is started.
This can be useful for ex. showing some data on opening a new cmd window or always change to a choosen directory.
But this batch will be also executed inside the FOR /F and normally causes unexpected results.
Pipes also start new cmd instances, but suppress the AutoRun script

Batch file which executes an exe from a specific path

I am trying to create a batch file which runs an exe from a specific path.
Eg: I have my exe in E drive. Exact path is E:\kk.exe. I want to run this kk.exe from D:\bin folder.
I use the following command in my batch file:
start "D:\bin" "E:\kk.exe"
So far no luck. Any help would be appreciated.
start "" /d "d:\bin" "e:\kk.exe"
start command has a peculiar behaviour: the first quoted argument is the title of the window. That is the reason for the initial "" (you can include the title you want). The rest of the line is the starting folder (/d, what will be the current active folder for the started process) and the command to execute.
cd /d "D:\bin"
start "window name" "E:\kk.exe"
If I've decoded your meaning correctly, you wish to run kk.exe while your current directory is d:\bin. This will create an independent process to run that program.
Note: the syntax of "start" is such that it's advisable to assign a window title (the first quoted parameter) - if you don't ant a title, leave the text out and use en empty quoted string.
However, if you just want to execute e:\kk.exe then
cd /d "D:\bin"
"E:\kk.exe"

Resources