Working with Environment Variables
nnCron can set environment variables and use them in its work. nnCron is actually a fully-functional manager of environment variables, and it can set both local environment variables (available only to programs started by nnCron) and system (global) environment variables, available to the all the software installed on your computer.
Environment variables defined in crontabs are totally independent of the system, and this is their main advantage. For example, in Windows NT/2000/XP, nnCron is run by SYSTEM, and variables of "user" SYSTEM may be different from variables set for a current user. And if the variables are defined in crontabs, they are not affected by this problem and you won't have reenter them after reinstalling your system.
System environment variables, created by nnCron, can be used in tasks, *.cmd and *.bat files, as well as any other programs. Local environment variables can be used in those command and batch files and programs which are started by nnCron.
How to Set System Environment Variables
System (global) environment variables are set with word SYS-SET.
SYS-SET varname=string value until the end of line # or SYS-SET varname="string value"
You can use word SYS-SET in any place of crontab with one condition: it must be outside of any task definitions.
# declaring a local variable SYS-SET PC_NAME=your friendly PC #( pc_hello Time: START-TIME Action: \ displaying a message box MSG: "This is %PC_NAME%!" )#
System environment variables are used for storing permanent, rarely changed values (e.g. paths to programs and system directories or global macrosubstitutions). System variables are reset each time crontabs are reloaded.
It must be noted that deleting a system variable from a crontab won't automatically remove it from system settings. Values of system environment variables are stored in system registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment), and you should remove obsolete values manually or use a special system tool System Properties - Advanced - Environment Variables (Windows 2000 and Windows XP).
How to Set Local Environment Variables
Local environment variables are set with word SET.
SET varname=string value until the end of line # or SET varname="string value"
You can use word SET in any place of crontab with one condition: it must be located outside of any task definitions.
# declaring a local variable SET PC_NAME=your friendly PC #( pc_hello Time: START-TIME Action: \ displaying a message box MSG: "This is %PC_NAME%!" )#
Local environment variables are much more flexible then system (global) variables. They are reloaded, (in other words, re-set) each time any program is started by nnCron. This allows a user to set values which are difficult to set with the standard means provided by a command interpreter. For example, you can set date, time or another changing factor an environment variable:
SET CURTIME=%hh%:%mm% SET CURDATE=%DD%.%MM%.%YY% SET WEEKDAY=%WD%
As you can see, nnCron allows to use its own variables when setting values of environment variables. Therefore, it is even possible to create "dynamic" environment variables, whose values will change as conditions change.
For example, one can declare a variable MYHOST containing a name of a host available at the moment:
# declaring an environment variable SET MYHOST=%MyHost% <% \ creating a new word : MyHost \ checking if the first host is accessible HOST-EXIST: "HOST1" IF \ the first host is accessible, \ returning its name S" \\HOST1\DIR1" ELSE \ checking if the second host is accessible HOST-EXIST: "HOST2" IF \ the second host is accessible, \ returning its name S" \\HOST2\DIR2" ELSE \ no hosts are accessible, \ returning an empty string S" " THEN THEN ; %>
For this purpose, we created word MyHost, which returns a name of an available host. If none of the specified hosts can be accessed at the moment, word MyHost returns an empty string.
Since environment variables declared with word SET are set anew each time a new application is started, string SET MYHOST=%MyHost% means that at the moment when any application starts, the value of environment variable MYHOST will be equal to the result returned by MyHost, i. e. a name of a host accessible at the moment or an empty string.
Please note that values of variables are "calculated" right at the moment when they are addressed, so it would be not a good idea to use constructs like the one below:
SET PATH="%PATH%;c:\xxx"
it will cause the value of PATH to become longer and longer each time PATH is addressed.
Additional information regarding local environment variables:
Inside a task (and in Forth Console) a value of an environment variable can be accessed with the help of word ENV ( aname u1 -- aval u2)
S" PATH" ENV TYPE CR
There also exists word SETENV ( aval u1 aname u2 -- ior )
S" c:\dos;c:\bin" S" PATH" SETENV THROW
How to Use Environment Variables in Tasks
Although the methods used to set local and system (global) variables are different, both types of variables are used in the same way in tasks. The general rule is as follows: in order to access a value of a variable, one just has to place its name between %...% characters.
Examples:
\ displaying a message box with a value of a variable MSG: "WINDIR=%WINDIR%" \ writing a value of a variable to a file FILE-WRITE: "c:\temp\test.txt" "%SCITE_HOME%"
Environment variables set on system level outside of nnCron (e.g. in autoexec.bat), can be used in tasks in the same way.