Home > Blockchain >  Windows batch file if else variable expansion gives "was unexpected at this time."
Windows batch file if else variable expansion gives "was unexpected at this time."

Time:06-25

This should be so simple: I don't normally want C:\MinGW\bin\gcc in my PATH, but when I do, I can never remember what to add, so I want a simple batch file named addGccToPath.bat. For starters, I created this simple batch file:

@echo off
set PATH=C:\MinGW\bin;c:\MinGW\bin\gcc;%PATH%

That works, but if I accidentally call it multiple times, my PATH variable keeps getting needlessly longer. So then I thought I'd be clever & make it conditional:

@echo off
if not defined PATH_BEFORE_ADDING_GCC (
   set PATH_BEFORE_ADDING_GCC=%PATH%
   set PATH=C:\MinGW\bin;c:\MinGW\bin\gcc;%PATH_BEFORE_ADDING_GCC%
   echo Added GCC to your path.
) else (
   echo GCC was already added to your path.
)

But when I run it, I get this:

C:\Users\minichm>addgcctopath
\Windows was unexpected at this time.

C:\Users\minichm>

I suspect this is because my PATH variable contains the text "C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\", and the end-parentheses after "x86" terminates the 'if' clause, and the following text "\Windows Kits\8.1..." confuses it, with the resulting error as shown.

How can I conditionally add text to an existing environment variable that already contains parentheses?

CodePudding user response:

The ever-loving delayed expansion trap

PATH_BEFORE_ADDING_GCC is not set when the if statement is parsed, so it will be replaced by nothing in the first set path= instruction.

path will then be replaced by %PATH:~1,-1%

Try:

@echo off
if defined PATH_BEFORE_ADDING_GCC (
 echo GCC was already added to your path.
 goto alreadydone
)
set "PATH_BEFORE_ADDING_GCC=%PATH%"
set "PATH=C:\MinGW\bin;c:\MinGW\bin\gcc;%PATH%"
set "PATH=%PATH:~1,-1%"
echo Added GCC to your path.
:alreadydone

Although I'd question the set "PATH=%PATH:~1,-1%" as it will delete the c of the c:... and delete the last character of the original path.

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier.

CodePudding user response:

Wrap the value in " so that special characters aren't treated as special

set "PATH_BEFORE_ADDING_GCC=%PATH%"
set "PATH=C:\MinGW\bin;c:\MinGW\bin\gcc;%PATH_BEFORE_ADDING_GCC%"

In fact you should always use the set "var=value" form because it'll prevent accidental cases where one leave blank spaces at the end which will eventually go into the variable

CodePudding user response:

It seems your problem are the whitespaces inside the path you added. You can solve this with quotes, but then you have to remove them from your PATH. Tell my if this works:

@echo off
if not defined PATH_BEFORE_ADDING_GCC (
   set PATH_BEFORE_ADDING_GCC=%PATH%
   set PATH="C:\MinGW\bin;c:\MinGW\bin\gcc;%PATH_BEFORE_ADDING_GCC%"
   set PATH=%PATH:~1,-1%
   echo Added GCC to your path.
) else (
   echo GCC was already added to your path.
)
  • Related