0

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.

2
  • 2
    Please confirm your posted code - preferably cut-and-paste the original. I observe in case A) for instance that you've posted AS in the code-path, not A S, yet you claim that there was a response (presumably from the executable) so therefore the executable was found. The response would seem to indicate that the quotes are being incorrectly interpreted - I'd remove the cmd/c and its surrounding quotes and dispose of the double-" myself - but I can'te test it since I don't have the executable in question. Commented Jun 11, 2017 at 18:04
  • I tried to do as you suggested, but the txt file was empty. I can't add the exe file because it's given to me as a black box. Commented Jun 18, 2017 at 15:19

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

When I run the batch file from the command line I do see the logs from the exe, but the txt file is empty. I want to be able to check if the phrase * * * Complete * * * appears in the logs, to make sure that the exe has succeeded. That's why I thought of redirecting the logs to a file (and then search for the phrase in the file). I'm open to other ideas..
With >"%USERPROFILE%\Desktop\Logs.txt" 2>&1 it is actually not possible that something is printed to screen when Image.exe is a real Windows console application using the standard handles stdout and stderror. So it looks like Image.exe is not a standard console application and therefore I'm unable to help you without having this executable or knowing more about it. It could be a batch file "compiled" (packed) into an executable, it could open itself a new command process to output its messages, it could use other handles, ... I can only guess and that is always a waste of time.
0

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.

2 Comments

When I run the batch file from the command line I do see the logs from the exe, but the txt file is empty. I want to be able to check if the phrase * * * Complete * * * appears in the logs, to make sure that the exe has succeeded. That's why I thought of redirecting the logs to a file (and then search for the phrase in the file). I'm open to other ideas..
In a DOS box I'd go to the directory your Image.exe program is in and type Image.exe >nul 2&>nul -- if anything appears on the screen you know the output of Image.exe cannot be redirected to a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.