Variables in shortcuts?

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
I have set up a Windows 7 variable that I want to use to designate a given browser, so that I change the var and all references to that var will then switch to the new browser.

So I set the var in My Computer / Advanced System Settings / Environment Variables. It shows when I use the SET command, so it's there (?).

But then, how would I use that var in a Windows shortcut? I tried %var% in the target, but I get an error.

How can I do this?

Regards,
Chuck Billow
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,482
44
Albuquerque, NM
prospero.unm.edu
#2
I have set up a Windows 7 variable that I want to use to designate a given browser, so that I change the var and all references to that var will then switch to the new browser.

So I set the var in My Computer / Advanced System Settings / Environment Variables. It shows when I use the SET command, so it's there (?).

But then, how would I use that var in a Windows shortcut? I tried %var% in the target, but I get an error.
%VAR% in a shortcut works for me. What are you doing that produces an error message, and what is the message?
 
#3

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,482
44
Albuquerque, NM
prospero.unm.edu
#4
All of that works fine for me, using %BROWSER% in place of the full path to Chrome.exe. Windows resolves the environment variable and starts the browser, as expected.

However, I believe you're mistaken in thinking that changing the contents of the environment variable will affect the behavior of the shortcut. It seems that once Windows has resolved the shortcut, it remembers the expanded form, and doesn't have to expand the environment variable any more.

It would probably be simpler to create a shortcut to the web page (a URL file) and let Windows open that with the currently registered browser.
 
#5
You have some type of magic touch, Charles. After I read your post, I went back and tried it again...and this time it worked like a charm!

It did work to change the variable and Windows automatically switched browsers without any further help from me [rebooting etc.].

That's cool...it does exactly what I had hoped.

Thanks Charles,
Chuck Billow
 
#6
OK, everything is working as planned. NOW...

Can I set a batch file to set/change that VAR? I tried running

SETX LFMBROWSER=
SETX LMBROWSER=C:\WinApps\Comm\Internet\Browsers\Firefox\firefox.exe
SETX LFMBROWSER
pause

But it doesn't seem to be "setting". I've tried set and setx with no progress.

It is some typo or detail I am missing?

Regards,
Chuck Billow
 
#7
If I read the help correctly (SETX /?), SETX does not use '=' between the variable name and its value.

SETX LFMBROWSER C:\WinApps\Comm\Internet\Browsers\Firefox\firefox.exe

TCC's "SET /U" can do the same thing and does use the '='.

SET /U LFMBROWSER=C:\WinApps\Comm\Internet\Browsers\Firefox\firefox.exe
 
#8
The important thing is that the program that's going to start the browser knows there has been a change in the (user) environment. In the case of a normal Windows shortcut that app is Explorer.exe, and it should work because both SETX and TCC's SET /U announce that a change has been made and Explorer listens to those announcements (see below). If you're relying on some other program (like a toolbar app) there's no guarantee it will know when the user environment has changed. As I said, Explorer gets it right. Below, after setting the variable, I'm looking at Explorer's environment (updated because of the change); not all programs do that.

Code:
v:\> setx foo bar
 
SUCCESS: Specified value was saved.
 
v:\> pset %@pid[explorer.exe] foo
bar
 
v:\> setx foo foo
 
SUCCESS: Specified value was saved.
 
v:\> pset %@pid[explorer.exe] foo
foo
 
v:\> set /u foo=bar
 
v:\> pset %@pid[explorer.exe] foo
bar
 
v:\> set /u foo=foo
 
v:\> pset %@pid[explorer.exe] foo
foo
 
#11
OK, part 2: If I set a variable [LFMBROWSER] for a browser, then my shortcut will have %LFMBROWSER% for the Target. Is there a way to set a variable that would then set the "Start In" path to match that of %LFMBROWSER% -- so that if the latter changes, the former will as well to match it?

Also, the icon for a shortcut for LFMBROWSER seems to most times set automatically to match the value (program) set by LFMBROWSER.

But then sometimes I open the folder containing that shortcut, and the icon has changed to just a, for lack of a better word, blank sheet of paper. It doesn't seem to be exactly what sometimes shows up in Windows when there is some icon issue.

Is there a way to "guarantee" that the icon will match the browser set by the variable LFMBROWSER? If I go to "fix" one that is wrong, it immediately goes to the browser that is dictated by the variable. So why doesn't the icon stay selected? Would I need to set two separate variables - one for the browser, one for the folder - for each change in browser?

And then...

I remember in the DOS days [yea, I'm that old] that 4dos.com was part of config.sys and became the active shell. That way variables, definitions, aliases etc. could be implemented in the autoexec file.

How do I accomplish the same result in Windows (7)? Would I have to set the vars each time I execute the btm file? If my shortcut is to set the browser variable to Chrome for instance, and it does so by executing a btm file with the command-line in it of

SET LFMBROWSER="C:\Users\CWBillow\AppData\Local\Google\Chrome\Application\chrome.exe"

What do I do, prefix the command with "tcc" in order for it to execute in tcmd / tcc? Would that then set all the tcmd variables and settings for the btm file to be able to use?

Regards,
Chuck Billow
 
#12
To the first question, probably not. To the second question, I don't know. To the third question, if you're using TCC to set LFMBROWSER, do it like this:

SET /U /E LFMBROWSER=...

That will set it in the current user's (registry) environment (/U) as well as TCC's current environment (/E). When TCC sets something in the user environment (/U) it announces that fact to all running apps. Explorer (few other programs) will get the message and update its own environment to reflect the change (but I doubt that'll help with the icon problem).

TCC's SET /U and SET /M are equivalent to MyComputer(left-click)\Properties\Advanced\EnvironmentVariables (user and machine environments). Anything so set is remembered and reinstated on a restart (machine) or a login (user).
 
#14
Does it "screw things up" if I set a variable that is already set? Is there some type of If/Then that could be used to query a variable and set it if it isn't and leave it if it is?

Chuck
 
#15
Does it "screw things up" if I set a variable that is already set? Is there some type of If/Then that could be used to query a variable and set it if it isn't and leave it if it is?
Chuck
It won't screw anything up; it'll just set it to a new (or even the same) value. And, yes ...

Code:
iff not defined varname then
    set varname=value
endiff
or more simply

Code:
if not defined varname set varname=value