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?