| View previous topic :: View next topic |
| Author |
Message |
Hooligan VDS Developer


Joined: 28 Oct 2003 Posts: 480 Location: California
|
Posted: Fri Oct 31, 2003 6:45 pm Post subject: Environment variables |
|
|
Has anyone had any luck setting and/or using system environment variables within your scripts? I am running W2k & XP...
Thanks...  _________________ Hooligan
Why be normal? |
|
| Back to top |
|
 |
Skit3000 Admin Team

Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
|
| Back to top |
|
 |
bbelcher Contributor

Joined: 30 Jul 2002 Posts: 172
|
Posted: Fri Oct 31, 2003 8:36 pm Post subject: |
|
|
You can find the system varibles in the
win2k key
HKEY_LOCAL\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
NT 4.0 is most likly the same. |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Fri Oct 31, 2003 8:43 pm Post subject: |
|
|
Use the dos SET command do set environment variables.
| Quote: | Displays, sets, or removes cmd.exe environment variables.
SET [variable=[string]]
variable Specifies the environment-variable name.
string Specifies a series of characters to assign to the variable.
Type SET without parameters to display the current environment variables.
If Command Extensions are enabled SET changes as follows:
SET command invoked with just a variable name, no equal sign or value
will display the value of all variables whose prefix matches the name
given to the SET command. For example:
SET P
would display all variables that begin with the letter 'P'
SET command will set the ERRORLEVEL to 1 if the variable name is not
found in the current environment.
SET command will not allow an equal sign to be part of the name of
a variable.
Two new switches have been added to the SET command:
SET /A expression
SET /P variable=[promptString]
The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated. The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:
() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator
If you use any of the logical or modulus operators, you will need to
enclose the expression string in quotes. Any non-numeric strings in the
expression are treated as environment variable names whose values are
converted to numbers before using them. If an environment variable name
is specified but is not defined in the current environment, then a value
of zero is used. This allows you to do arithmetic with environment
variable values without having to type all those % signs to get their
values. If SET /A is executed from the command line outside of a
command script, then it displays the final value of the expression. The
assignment operator requires an environment variable name to the left of
the assignment operator. Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.
The /P switch allows you to set the value of a variable to a line of input
entered by the user. Displays the specified promptString before reading
the line of input. The promptString can be empty.
Environment variable substitution has been enhanced as follows:
%PATH:str1=str2%
would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2". "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output. "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.
May also specify substrings for an expansion.
%PATH:~10,5%
would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result. If the length is not specified, then it defaults to the
remainder of the variable value. If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.
%PATH:~-10%
would extract the last 10 characters of the PATH variable.
%PATH:~0,-2%
would extract all but the last 2 characters of the PATH variable.
Finally, support for delayed environment variable expansion has been
added. This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?
Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)
would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal. Similarly, the following example
will not work as expected:
set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%
in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:
for %i in (*) do set LIST= %i
which just keeps setting LIST to the last file found.
Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)
set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%
If Command Extensions are enabled, then there are several dynamic
environment variables that can be expanded but which don't show up in
the list of variables displayed by SET. These variable values are
computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then
that definition will override the dynamic one described below:
%CD% - expands to the current directory string.
%DATE% - expands to current date using same format as DATE command.
%TIME% - expands to current time using same format as TIME command.
%RANDOM% - expands to a random decimal number between 0 and 32767.
%ERRORLEVEL% - expands to the current ERRORLEVEL value
%CMDEXTVERSION% - expands to the current Command Processor Extensions
version number.
%CMDCMDLINE% - expands to the original command line that invoked the
Command Processor. |
_________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
Skit3000 Admin Team

Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
|
| Back to top |
|
 |
Hooligan VDS Developer


Joined: 28 Oct 2003 Posts: 480 Location: California
|
Posted: Mon Nov 03, 2003 2:38 am Post subject: |
|
|
Thanks Skit, (for the @env() reminder...)
As for setting them... using the cmd (dos) set command only sets it for the current session/user. I need to set them for all users... I have tried to set them in the registry, as bbelcher suggested, but they were not accessible to other programs. Although, when I go into system properties I can see them. Now, if I open it for editing in this sysprop window and save out of there immediately, the other programs can now use it!?!?! Go figure! I have also been able to use MS's setx command (from their website) successfully, but it too is painful! (Does this make sense?)
Thanks... _________________ Hooligan
Why be normal? |
|
| Back to top |
|
 |
Protected Valued Contributor


Joined: 02 Jan 2001 Posts: 228 Location: Portugal
|
Posted: Mon Nov 03, 2003 9:08 am Post subject: |
|
|
| Why exactly do you want to use environment variables? |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Mon Nov 03, 2003 11:34 am Post subject: |
|
|
You might use the following APIs to get what you need done:
(If you need help there's plenty )
| Code: | SetEnvironmentVariable
GetEnvironmentVariable |
_________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
Hooligan VDS Developer


Joined: 28 Oct 2003 Posts: 480 Location: California
|
Posted: Mon Nov 03, 2003 1:25 pm Post subject: |
|
|
| Protected wrote: | | Why exactly do you want to use environment variables? | Mostly because I was told to... Why do "they" want to, to set up paths for their apps...
FreezingFire,
If you have a simple example, I'd be eternally grateful...
Thanks again...  _________________ Hooligan
Why be normal? |
|
| Back to top |
|
 |
Protected Valued Contributor


Joined: 02 Jan 2001 Posts: 228 Location: Portugal
|
Posted: Mon Nov 03, 2003 3:19 pm Post subject: |
|
|
Do you plan to use DOS programs or anything? There are lots of other methods you can use with windows with advantage... From the registry, to INI files, or semi-weird custom methods like having a background app running to retransmit information between programs or having them communicate directly through DDE or something I dont really know, I just can't see a good reason to use environment variables in windows =) Although I used to use them a lot in DOS... |
|
| Back to top |
|
 |
Hooligan VDS Developer


Joined: 28 Oct 2003 Posts: 480 Location: California
|
Posted: Tue Nov 04, 2003 1:33 pm Post subject: |
|
|
Actually, we use both ini's and the reg., but the reason we need an env. var. is because we access the ini's through ethernet from a PowerPC sys. running VxWorks OS, but the location of the ini's may vary from system to system... If we were dealing with a pure windows system, it would be easy... (is "pure windows" an oxymoron?)
Thanks for the time and effort you've been putting in!  _________________ Hooligan
Why be normal? |
|
| Back to top |
|
 |
bbelcher Contributor

Joined: 30 Jul 2002 Posts: 172
|
Posted: Tue Nov 04, 2003 1:55 pm Post subject: |
|
|
what about this key
HKEY_CURRENT_USERS\Environment
This will add a varible for what ever profile logs in.
This sets a user varible for %USERPROFILE%. Not system |
|
| Back to top |
|
 |
jules Professional Member


Joined: 14 Sep 2001 Posts: 1043 Location: Cumbria, UK
|
Posted: Tue Nov 04, 2003 2:44 pm Post subject: |
|
|
I don't think it is possible to set an environment variable and have the change take effect globally. It needs to happen at start-up, e.g. in a log-on script.
Back in the days when DOS was considered an operating system (aah, happy days!) I remember seeing code that would do this. It walked through memory looking for the environment blocks and then modified them. Those, of course, were the days when you only had 640KB to play with and there was no such thing as protected memory.
Probably sounds like the dinosaur age to many members of this forum.  _________________ The Tech Pro
www.tech-pro.net |
|
| Back to top |
|
 |
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1753 Location: Space and Time
|
Posted: Tue Nov 04, 2003 2:47 pm Post subject: |
|
|
Ah...I remember thoes times...  _________________ Chris
Http://theblindhouse.com |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Tue Nov 04, 2003 5:03 pm Post subject: |
|
|
| jules wrote: | I don't think it is possible to set an environment variable and have the change take effect globally. It needs to happen at start-up, e.g. in a log-on script.
Back in the days when DOS was considered an operating system (aah, happy days!) I remember seeing code that would do this. It walked through memory looking for the environment blocks and then modified them. Those, of course, were the days when you only had 640KB to play with and there was no such thing as protected memory.
Probably sounds like the dinosaur age to many members of this forum.  |
Umm Jules,
It is possible. I remember a MS utility that came with one of the resource kits for Win9x or NT 4.0. I can't remember which. Also I do have C source code that can do this too but I have been too busy to implement this into a VDS DLL as of yet. To get the MS utility you could do a search on MSDN's website. I think it is mentioned there. It's a console command line only utility.
| MSDN wrote: | Calling SetEnvironmentVariable has no effect on the system environment variables. The user can add or modify system environment variables using the Control Panel. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message. This allows applications, such as the shell, to pick up your updates.
|
The quote above is from MSDN but I am not sure if that registry setting is on all versions of windows Also to send the message as a broadcast message you would have to use %%HWND_BROADCAST = %65535 for the Window ID in the @Sendmsg call...
%%WM_SETTINGCHANGE = 26
The registry key above is under Win2k Pro. _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
|