Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:32:22+00:00 2026-06-09T20:32:22+00:00

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

  • 0

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)% then echo what it was set to:

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

However when putting this same command into a shortcut, I get this:

Command Prompt showing ProgramFiles(x32) was set prior, but is not being seen by subsequent echo

What is going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:32:24+00:00Added an answer on June 9, 2026 at 8:32 pm

    First I’ll start from the perspective of running your code from the command prompt. You have 4 problems with your code in that context.

    1) I don’t think this makes much difference, but I believe you always want to execute the last 3 commands in your command line. But you used the conditional && operator which only executes the following command if the preceding command was successful. I believe you want to use & instead.

    2) You have a precedence problem. You want to execute an IF/ELSE statement, then when it has completed you want to execute 3 additional commands. But your 3 additional commands as written are considered to be part of the ELSE clause of the IF statement. You need an extra set of parentheses around the entire IF/ELSE statement.

    (if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "ProgramFiles(x32)=%programfiles(x86)%")) & ...
    

    3) You want everything after %comspec% /c to be part of the command line that is passed to CMD.EXE. But when you run your entire statement in a command line console the call to CMD.EXE ends at the first appearance of &. The remainder of the line is executed by the parent command window, not by your call to CMD.EXE. You need to either escape the & characters as ^&, or else quote the entire line that is to be passed.

    4) %VAR% expansion occurs when a line is parsed, and the entire command line is parsed at one time before any of the commands are executed. So the value of %ProgramFiles(x32)% that you see is the value before any of the commands are executed. The value is normally not defined, and you are in a command line context, so the original string with the percents is printed. The fix is to either use CALL ECHO %^ProgramFiles(x32)% or else enable delayed expansion with the /V:ON option and use ECHO !ProgramFiles(x32)!. The caret is to ensure that the string is passed to CMD.EXE and not expanded in your parent command line (just in case ProgramFiles(x32) is already defined).

    Each of the following should give the result you are looking for from a command prompt:

    %comspec% /v:on /c "(if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) & set Program & echo !programfiles(x32)!& pause"
    
    %comspec% /c "(if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) & set Program & call echo %^programfiles(x32)%& pause"
    
    %comspec% /v:on /c (if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) ^& set Program ^& echo !programfiles(x32)!^& pause
    
    %comspec% /c (if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) ^& set Program ^& call echo %^programfiles(x32)%^& pause
    

    The situation is a bit different when executed from a shortcut. The shortcut does not have a parent command prompt, so the point 3) above does not apply. The & characters should not be escaped, though the entire command line may still be quoted.

    Points 1, 2, and 4 are still valid for the shortcut.

    So the following statements will all work as a shortcut:

    %comspec% /v:on /c "(if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) & set Program & echo !programfiles(x32)!& pause"
    
    %comspec% /c "(if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) & set Program & call echo %^programfiles(x32)%& pause"
    
    %comspec% /v:on /c (if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) & set Program & echo !programfiles(x32)!& pause
    
    %comspec% /c (if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) & set Program & call echo %^programfiles(x32)%& pause
    

    Comparing the 2 sets of statements, only 2 of them are valid for both contexts. The following 2 statements will work both on the command line and in a shortcut:

    %comspec% /v:on /c "(if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) & set Program & echo !programfiles(x32)!& pause"
    
    %comspec% /c "(if exist "%programfiles%" (set "ProgramFiles(x32)=%programfiles%") else (set "%ProgramFiles(x32)=%programfiles(x86)%")) & set Program & call echo %^programfiles(x32)%& pause"
    

    Both statements above give the following result when issued from a Command Prompt or as a shortcut on my Vista machine:

    ProgramData=C:\ProgramData
    ProgramFiles=C:\Program Files
    ProgramFiles(x32)=C:\Program Files
    ProgramFiles(x86)=C:\Program Files (x86)
    C:\Program Files
    Press any key to continue . . .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems so simple, but has turned out to be such a pain. On
This seems simple but I can't figure it out. I receive post data in
This seems painfully simple, but I can't work out how to do it: I
This seems simple, but I can't figure it out. I have ten categories and
This seems really simple but I can't see that NSNumberFormatter has a function for
This seems like a simple question, but has proven to be difficult to find
I'm sure this question has a simple enough answer, but I can't seem to
This seems like it should be very simple but I can't get it to
This seems like such a simple issue but I cannot find an elegant solution.
this seems like a simple enough question but I can't seem to find a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.