S.M
S.M

Reputation: 1

Redirect output from exe with parameters through batch file

I need to run an .exe file through a batch file and redirect its output to a text file. The problem is that the path of the exe and the parameters that it gets have many spaces and backslashes and I can't get it to work. It either creates a blank txt file, or doesn't run my command at all (parses the command incorrectly).

The exe path: C:\Program Files (x86)\A S\Tools\Image\Image.exe
The parameters:
1) -v
2) C:\Program Files (x86)\A S\MyFiles\file_00_00_65
3) /0 /1 /2 /3 /5
4) C:\Program Files (x86)\A S\MyFiles\file_00_00_65\Images\AB.dtb

I have tried the following solutions:

A)

    start "" "cmd /c ""C:\\Program Files (x86)\\AS\\Tools\\Image\\Image.exe" -v "C:\\Program Files (x86)\\A S\\MyFiles\\file_00_00_65" /0 /1 /2 /3 /5 "C:\\Program Files (x86)\\A S\\MyFiles\\file_00_00_65\\Images\\AB.dtb"" > Logs.txt 2>&1"

It fails: "C:\Program Files (x86)\A S\MyFiles\file_00_00_65\Images\AB.dtb"" couldn't be found.

B)

    start "" cmd /c ""C:\\Program Files (x86)\\AS\\Tools\\Image\\Image.exe" -v "C:\\Program Files (x86)\\A S\\MyFiles\\file_00_00_65" /0 /1 /2 /3 /5 "C:\\Program Files (x86)\\A S\\MyFiles\\file_00_00_65\\Images\\AB.dtb"" > Logs.txt 2>&1

It fails: Creates an empty txt file.

C)

    >Logs.txt 2>&1 (
     start "" cmd /c ""C:\\Program Files (x86)\\A S\\Tools\\Image\\Image.exe" -v "C:\\Program Files (x86)\\A S\\MyFiles\\file_00_00_65" /0 /1 /2 /3 /5 "C:\\Program Files (x86)\\A S\\MyFiles\\file_00_00_65\\Images\\AB.dtb"" 
     )

It fails: "\A was unexpected at this time."

D)

    cmd /c ""C:\\Program Files (x86)\\A S\\Tools\\Image\\Image.exe" -v "C:\\Program Files (x86)\\A S\\MyFiles\\file_00_00_65" /0 /1 /2 /3 /5 "C:\\Program Files (x86)\\A S\\MyFiles\\file_00_00_65\\Images\\AB.dtb"" > Logs.txt | type Logs.txt

It fails: Creates an empty txt file.

I have also tried start /B /wait... and more.

Upvotes: 0

Views: 2328

Answers (2)

Paul Houle
Paul Houle

Reputation: 735

Not sure why you are bothering with start or cmd if you are willing to wait for the program to complete. If it's just argument passing confusion, something along this line should work:

set a1="C:\Program Files (x86)\A S\MyFiles\file_00_00_65" "/0 /1 /2 /3 /5"
set a2="C:\Program Files (x86)\A S\MyFiles\file_00_00_65\Images\AB.dtb"
"C:\Program Files (x86)\A S\Tools\Image\Image.exe" %a1% %a2% >Logs.txt 2>&1

The use of environment variables a1 and a2 here is simply to manage the line length so it's visible w/o scrolling -- you can remove them and make it all one line if you wish.

I have quoted "/0 /1 /2 /3 /5" since you say that is a separate parameter (as opposed to parameters). If those are instead five individual switches those quotes should be removed.

Upvotes: 0

Mofi
Mofi

Reputation: 49097

What about following command line in your batch file?

"%ProgramFiles(x86)%\A S\Tools\Image\Image.exe" -v "%ProgramFiles(x86)%\A S\MyFiles\file_00_00_65" /0 /1 /2 /3 /5 "%ProgramFiles(x86)%\A S\MyFiles\file_00_00_65\Images\AB.dtb" >"%USERPROFILE%\Desktop\Logs.txt" 2>&1

In batch files the backslash character must not be escaped with another backslash. That is only necessary in many programming and scripting languages, but not in batch files.

The command START runs a console application in a new command process.

The command CMD starts also a new command process.

But the batch file is already running in a command process.

So I really don't understand from what is written currently in question why making it more complicated than necessary and use additionally the commands START and CMD.

The file Logs.txt is created on your desktop with that command line to use in a batch file or in a shortcut file (*.lnk).

Of course the application Image.exe must be a console application writing its output to handle STDOUT and its errors to handle STDERR for being redirected both to file Logs.txt. Redirecting the output of a Windows GUI application to a file is not possible from command line.

Upvotes: 2

Related Questions