0

If you execute this:

if "true" == "true" (
    echo Path is C:\Program Files (x86)\MyFolder
)

You will get an error:

\MyFolder was unexpected at this time.

This is the same if a path is passed to echo as a variable.

There is a solution. This can be fixed by surrounding the text for echo with double quotes (or just the path itself).

However this adds double quotes to the output text:

Path is "C:\Program Files (x86)\MyFolder"

Is there any way to handle this situation without altering output?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
InfernumDeus
  • 1,185
  • 1
  • 11
  • 33
  • 2
    You need to prevent any internal closing parenthesis from being read as the closing parenthesis of the code block. You can escape that internal parenthesis using the standard escape character, the caret, **`^`**: `If /I "true"=="true" (Echo Path is C:\Program Files (x86^)\MyFolder)`. – Compo Sep 12 '19 at 08:28
  • @Compo, it works. Thank you! I guess you can move it to anwer. – InfernumDeus Sep 12 '19 at 08:31
  • Though, it doesn't help if I echo an unexpected variable. – InfernumDeus Sep 12 '19 at 08:43
  • My comment works for your question, if your actual code, or the issue you're now reporting, is not part of the question, we're unable to see or guess it. Please use the [edit button](https://stackoverflow.com/posts/57902398/edit), to modify your question to a real world task with sufficient supporting information for us to assist you with your problem. – Compo Sep 12 '19 at 08:49
  • I think it is important to ask more detail around this. Are you doing multple if and else statemets? This makes a big difference in how we can give a solution. – Gerhard Sep 12 '19 at 09:31
  • Do not add solutions into the questions, post them as answers instead since that answers are for! Anyway, to echo an arbitrary string in any situation, store it in a variable and echo it using [delayed expansion](https://ss64.com/nt/delayedexpansion.html)... – aschipfl Sep 12 '19 at 09:33
  • 2
    @aschipfl I do not think he added it as a solution, he is more saying the solution to double quote the path is not a solution to his issue as it also adds double quotes to the result. :) – Gerhard Sep 12 '19 at 09:34
  • 1
    Agreed, @GerhardBarnard, sorry for not reading carefully enough... – aschipfl Sep 12 '19 at 11:18

2 Answers2

1

As already stated in the comment by @compo, the closing parenthesis is causing the issue as the system thinks it is the closure of your earlier opening parenthesis. You therefore need to escape the parenthesis ^)

However as stated by you, this does not help you when you have an unknown output result, I however do do really understand why the quoted path is problem in the output. but anyway, therefore you need to work around the issue, here are ways, depending on how your actual script layout.

First option, Do not use parenthesized if code block:

if "true" == "true" echo path is %ProgramFiles(x86)%\MyFolder

The above method however will not allow you to do else statements and you would need multple if statements.

The other, better option would be to simply set the variable if there is a match.. Depending on your code blocks, these might require delayedexpansion

if "true" == "true" (
     set "mypath=%ProgramFiles(x86)%\MyPath"
   ) else (
     set "mypath=%ProgramFiles(x86)%\AnotherPath"
 )
echo %mypath%
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

To echo an arbitrary text including special characters you could assign the text to a variable and then apply delayed variable expansion when echoing:

set "EchoPath=C:\Program Files (x86)\MyFolder"
setlocal EnableDelayedExpansion
if "true" == "true" (
    echo(!EchoPath!
)
endlocal

The setlocalcommand enables delayed expansion here, the exclamation marks around the variable actually use it. The endlocal command ends delayed expansion. Note that any changes to the variable EchoPath since setlocal become lost past endlocal.

The odd-looking syntax echo( is used here to avoid trouble with certain strings like on and off (which are special keywords), or a string like /?, all of which could theoretically be stored in the variable. Consult also the following threads about that:

aschipfl
  • 33,626
  • 12
  • 54
  • 99