Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using environment variables

37 views
Skip to first unread message

Dr J R Stockton

unread,
Feb 26, 2017, 6:45:57 PM2/26/17
to

From reading and programming, I am accustomed to the use of an
environment variable to provide all or part of an argument or option to
an executable; and, though I recall no instances, I expect that one
variable can provide multiple arguments ( Fx: test : OK).

But I recall seeing no instance of using an environment variable to
provide the command itself, with/without arguments/options
included/following/both. That might be powerful, unnecessary, or both.

Tests, in WinXP sp3 Command Prompt, indicate that it does work, at least
in simple cases such as

>set xx=dir /od
>%xx% Q*.*
&
>set xx=di
>%xx%R Q*.*

Questions : Is this type of usage specified by reasonable authority as
legitimate? Is it actually reliable in WinXP, Win 7, Win 10? Is it
useful? Are there traps?

--
(c) John Stockton, Surrey, UK. 拯merlyn.demon.co.uk Turnpike v6.05 MIME.
Merlyn Web Site < > - FAQish topics, acronyms, & links.


JJ

unread,
Feb 26, 2017, 10:50:12 PM2/26/17
to
On Sun, 26 Feb 2017 20:21:55 +0000, Dr J R Stockton wrote:
> From reading and programming, I am accustomed to the use of an
> environment variable to provide all or part of an argument or option to
> an executable; and, though I recall no instances, I expect that one
> variable can provide multiple arguments ( Fx: test : OK).
>
> But I recall seeing no instance of using an environment variable to
> provide the command itself, with/without arguments/options
> included/following/both. That might be powerful, unnecessary, or both.
>
> Tests, in WinXP sp3 Command Prompt, indicate that it does work, at least
> in simple cases such as
>
> >set xx=dir /od
> >%xx% Q*.*
> &
> >set xx=di
> >%xx%R Q*.*
>
> Questions : Is this type of usage specified by reasonable authority as
> legitimate? Is it actually reliable in WinXP, Win 7, Win 10? Is it
> useful? Are there traps?

Uh... that's pretty common use of environment variables. It's been used for
data storage, and string substitution which includes command template and
macro. It's pretty consistent since the DOS era.

John Gray

unread,
Feb 28, 2017, 12:42:07 PM2/28/17
to
I use this technique commonly with PsExec, which can run only on a remote computer. Hence code like:

:: set up PsExec parameters
set pse=%psepath%\psexec \\%pc% -h -accepteula cmd /c
:: but don't use PsExec on the computer on which we're running!
if /i "%pc%"=="%computername%" set pse=
:: perform route print for IPv4
%pse% route print -4 >> %log%
0 new messages