Search code examples
if-statementsetdos

Setting DOS Environment Variable to another Environment Variable in if statement


This seems so simple, but has turned out to be such a pain.

On Windows 7, I can paste the below into a command prompt and have it set ProgramFiles(x32) to either %programfiles% or %programfiles(x86)%:

%comspec% /c if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set %ProgramFiles(x32)=%programfiles(x86)%")

However on Windows XP, using the the same and others methods, I cannot get the same result:

E:\>%comspec% /c if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "ProgramFiles(x32)=%programfiles(x86)%")
E:\Program was unexpected at this time.

E:\>%comspec% /c if exist "%programfiles%" (set ProgramFiles(x32)=%programfiles%) else (set ProgramFiles(x32)=%programfiles(x86)%)
E:\Program was unexpected at this time.

E:\>%comspec% /c if exist "%programfiles%" (set "ProgramFiles(x32)"="%programfiles%") else (set "ProgramFiles(x32)=%programfiles(x86)%")
E:\Program was unexpected at this time.

E:\>%comspec% /c if exist "%programfiles%" (set ProgramFiles(x32)="%programfiles%") else (set ProgramFiles(x32)=%programfiles(x86)%)
"E:\Program Files") was unexpected at this time.

E:\>%comspec% /c if exist "%programfiles%" (set ProgramFiles(x32)=%%programfiles%%) else (set ProgramFiles(x32)=%programfiles(x86)%)
%E:\Program was unexpected at this time.

set ProgramFiles(x32)=%programfiles% works fine straight from the command line on both Win XP & 7.

What AM I missing?

As a side note, I REALLY wish Microsoft would have made %ProgramFiles% be the directory for 32 bit programs. I can't see the logic in having to do this checking, when it were that way a 32 bit program would be in the same location regardless of the bitage of the OS.


Solution

  • @dbenham Very in depth, and very much appreciated enlightenment in another question of mine, solves this one as well - Different behaviour of DOS Environment setting, in if statement, in DOS prompt as compared to Windows Shortcut