Ferdi
Ferdi

Reputation: 35

cmd new window store output

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()

Upvotes: 2

Views: 1619

Answers (3)

rojo
rojo

Reputation: 24486

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).

Upvotes: 1

Egor   Skriptunoff
Egor Skriptunoff

Reputation: 974

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"""]])

Upvotes: 0

jeb
jeb

Reputation: 82390

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).

Upvotes: 0

Related Questions