Different behaviour of DOS Environment setting, in if statement, in DOS prompt as compared to Windows Shortcut

时光毁灭记忆、已成空白 提交于 2019-12-25 01:38:55

问题


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:

What is going on?


回答1:


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 . . .


来源:https://stackoverflow.com/questions/11981073/different-behaviour-of-dos-environment-setting-in-if-statement-in-dos-prompt-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!