7

I want to launch a windows executable from a batch file where the path to the executable is stored in a variable.

@echo off

set qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin
set execpath=%qtpath%\windeployqt.exe

echo %execpath%

%execpath% --someparams

Unfortunately executing my script throws an error:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

Looks like somehow the string gets terminated at the space in Program Files.

2 Answers 2

15

You should change your code to this:

@echo off

set "qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin"
set "execpath=%qtpath%\windeployqt.exe"

echo "%execpath%"

"%execpath%" --someparams

The SPACE, like also TAB, ,, ;, =, VTAB (vertical tabulator, ASCII 0x0B), FF (form-feed, ASCII 0x0C) and NBSP (non-break space, ASCII 0xFF) constitute token separators in the command prompt cmd. To escape tokenisation enclose your path in between "". This avoids also trouble with special characters like ^, ( and ), &, <, > and |.

The quotation marks in the set command lines again avoid trouble with special characters; they do not become part of the variable value because they enclose the entire assignment expression. Note that this syntax requires the command extensions to be enabled, but this is the default anyway.

I recommend not to include the quotation marks into variable values (set VAR="some value"), because then you could run into problems particularly when concatenating strings due to unwanted (double-)quotation (for instance, echo "C:\%VAR%\file.txt" returns "C:\"some value"\file.txt").

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

Comments

2

You are perfectly right. If the path to the file you want to execute contains spaces, you have to surround it with quotation marks:

@echo off

set qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin
set execpath="%qtpath%\windeployqt.exe"

echo %execpath%

%execpath% --someparams

This should work.

It will also work when you surround %execpath% with quotation marks:

"%execpath%" --someparams

Comments

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.