In one of my scripts, I need to use variables that contain brackets inside IF , but either there is no closing bracket in the line, or the script exits prematurely with * was unexpected at this time (actually not an asterisk), depending on the scenario.
Example
@echo off SET path=%programFiles(x86)% echo Perfect output: %path% IF NOT "%path%" == "" ( REM Variable is defined echo Broken output: %path% ) pause >nul
Exit
Perfect output: C:\Program Files (x86) Broken output: C:\Program Files (x86
I think / know that this is because he believes that the closing bracket in C:\Program Files (x86) is the end of the IF , and it ends before echo completes.
Is there an easy way to do this? Preferably without resorting to
- single-line
IF , since I need to run several lines of code in them, - copious amounts of
GOTO s, as this is impractical, SETLOCAL EnableDelayedExpansion and using !path! instead of %path% , as I recall, I read somewhere that this method does not work consistently in the OS.
If not, I will gladly agree to the most reliable solution, whatever it may be.
(The script is not discussed. This is just a sophisticated, concentrated example of the problem. The structure should be as it is in my actual script, for reasons that I wonโt go to. This, moreover, and it just confuses things and distracts from actual problem.)
parentheses batch-file
mythofechelon Aug 14 2018-12-12T00: 00Z
source share