Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file variable with spaces and parentheses

I've read numerous threads on different approaches to getting the windows batch file parser to properly handle variables that have spaces, parentheses, and other special characters, but none of the recommendations seems to be able to fix the issue I am having.

Here is the script (prior to trying any workarounds), whose goal is to set a value for variable03 based on the values found for variable01 and variable02:

set variable01="C:\Program Files (x86)\SomeProgram\Subfolder"
set variable02="${macro}"

set variable01=%variable01:"=%
set variable02=%variable02:"=%

set variable03=""

if %variable02:~0,1%==$ (
   if %variable01:~0,1%==$ (
      set variable03=%variable03:"=%
   ) else (
      set variable03=-o '%variable01%'
   )
)

...the values of variable01 and variable02 are not known in advance - they are substituted by another program prior to running the script, so the above script is showing an example set of values for variable01 and variable02 after that substitution has been made.

The error I get when this script runs is:

\SomeProgram\Subfolder' was unexpected at this time.

...which corresponds to the last 'set' line in the above script. I assumed that this error was due to the parentheses in the value of variable01.

If I change that line to this:

set "variable03=-o '%variable01%'"

...then I get this error:

Files was unexpected at this time.

...which seems to indicate that it is trying to tokenize on the spaces in variable01, and the parser is still not happy.

If I then add this line at the top of the script:

 setlocal enableextensions enableDelayedExpansion

...and change %variable01% to !variable01!, I still get the same error.

Clearly, I do not understand what the batch file parser needs to meet my requirement that the value of variable03 has the following value:

-o 'C:\Program Files (x86)\SomeProgram\Subfolder'

...any suggestions?

like image 757
Hoobajoob Avatar asked Feb 25 '13 20:02

Hoobajoob


People also ask

How do you handle spaces in a batch file?

When you send arguments, those with poison or space characters need to be doublequoted. Inside your batch file, if you no longer need the surrounding doublequotes, you'd remove them by using %~5 instead of %5 . Additionally the recommended syntax for the set command is Set "VariableName=VariableValue" .

What does %% do in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

Can path variable have space?

Most programs separate their command line arguments with a space. But the PATH environment variable doesn't use spaces to separate directories. It uses semicolons.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.


2 Answers

As Nate wrote, the problem in this case are the brackets, but the complete code is still unstable.

It's always better to use delayed expansion, as this is safe against any special characters.
And you should use the extended syntax of SET set "variable=content" to enclose the complete expression with quotes, then it's nearly safe, and the quotes are not added to the content.
And you don't need to remove the quotes later.

This should work with any content in var1 and var2

setlocal EnableDelayedExpansion
set "variable01=C:\Program Files (x86)\SomeProgram\Subfolder"
set "variable02=${macro}"

set "variable03="

if "!variable02:~0,1!"=="$" (
   if "!variable01:~0,1!"=="$" (
      rem
   ) else (
      set "variable03=-o '!variable01!'"
   )
)
echo(!variable03!
like image 133
jeb Avatar answered Sep 28 '22 04:09

jeb


The problem is the parentheses in variable01's value. Since it's being expanded in an if condition, those parentheses are being interpreted as flow control. Fix by always putting it in double quotes.

set variable01="C:\Program Files (x86)\SomeProgram\Subfolder"
set variable02="${macro}"

set variable01=%variable01:"=%
set variable02=%variable02:"=%

set variable03=""

if "%variable02:~0,1%"=="$" (
   if "%variable01:~0,1%"=="$" (
      set variable03=%variable03:"=%
   ) else (
      call :set3 "%variable01%"
   )
)
goto :eof

REM If it is important to have single quotes instead of double then
REM I found I had to call a subroutine.  Otherwise the set could be
REM left up in the else.
:set3
set variable03=-o '%~1'
goto :eof
like image 35
Nate Hekman Avatar answered Sep 28 '22 03:09

Nate Hekman