Environment Variables in WSH JS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr John Stockton

    Environment Variables in WSH JS


    In javascript executed by Windows Scripting Host, one can input via
    command-line parameters : File $95.js :-
    x = WScript.Argumen ts
    WScript.echo("R esult:", x.length, typeof x, x[1], "OK")

    Prompt>cscript //nologo $95.js 66 77 88
    Result: 3 object 77 OK
    The Arguments are 0-based.

    Can one read/write Environment variables, and if so how? An answer for
    VBscript could be useful, as well or in lieu.


    Aside : <FAQENTRY> 2.11 : "over 5 years" is IMHO ambiguous. Suggest
    "more than 5 years" or "for 5 years", whichever is meant.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk DOS 3.3, 6.20; Win98. ©
    Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links.
    PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>
    My DOS <URL:http://www.merlyn.demo n.co.uk/batfiles.htm> - also batprogs.htm.
  • Yann-Erwan Perio

    #2
    Re: Environment Variables in WSH JS

    Dr John Stockton wrote:
    [color=blue]
    > Can one read/write Environment variables, and if so how? An answer for
    > VBscript could be useful, as well or in lieu.[/color]

    Use the WshShell object to read the Environment property:

    ---
    var ws, en, buf;

    ws=WScript.Crea teObject("WScri pt.Shell");
    en=new Enumerator(ws.E nvironment)
    buf=[];

    for(;!en.atEnd( );en.moveNext() )
    buf.push(en.ite m());

    WScript.Echo(bu f.join("\n"));
    ws=null;
    ---

    Reference:<URL: http://msdn.microsoft. com/library/default.asp?url =/library/en-us/script56/html/wsProEnvironmen t.asp>
    Download:<URL:h ttp://msdn.microsoft. com/library/default.asp?url =/downloads/list/webdev.asp>


    HTH
    Yep.

    Comment

    • Dr John Stockton

      #3
      Re: Environment Variables in WSH JS

      JRS: In article <3ffb288e$0$286 91$626a54ce@new s.free.fr>, seen in
      news:comp.lang. javascript, Yann-Erwan Perio <y-e.perio@em-lyon.com>
      posted at Tue, 6 Jan 2004 22:28:34 :-[color=blue]
      >Dr John Stockton wrote:
      >[color=green]
      >> Can one read/write Environment variables, and if so how? An answer for
      >> VBscript could be useful, as well or in lieu.[/color]
      >
      >Use the WshShell object to read the Environment property:
      >
      >---
      >var ws, en, buf;
      >
      >ws=WScript.Cre ateObject("WScr ipt.Shell");
      >en=new Enumerator(ws.E nvironment)
      >buf=[];
      >
      >for(;!en.atEnd ();en.moveNext( ))
      > buf.push(en.ite m());
      >
      >WScript.Echo(b uf.join("\n"));[/color]

      Thanks; that works for me (except for buf.push), and I can also now read
      in VBscript. But can Environment variables be written from these
      languages? To do it in Pascal, I had to work low-level.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk DOS 3.3, 6.20; Win98. ©
      Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links.
      PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>
      My DOS <URL:http://www.merlyn.demo n.co.uk/batfiles.htm> - also batprogs.htm.

      Comment

      • Yann-Erwan Perio

        #4
        Re: Environment Variables in WSH JS

        Dr John Stockton wrote:
        [color=blue]
        > But can Environment variables be written from these
        > languages? To do it in Pascal, I had to work low-level.[/color]

        Far from being a WSH specialist I'm afraid I cannot give you as a
        definite answer about this as I'd like to. Still, some quick lookup in
        the archives yielded the two following articles, which might be of
        interest for you:

        <URL:http://groups.google.c om/groups?hl=fr&lr =&ie=UTF-8&oe=UTF-8&selm=e52812c2 BHA.2588%40tkms ftngp07>
        <URL:http://groups.google.c om/groups?hl=fr&lr =&ie=UTF-8&oe=UTF-8&selm=3F54F46F .59CADE82%40hyd ro.com>

        The quick story is that you can set environment variables via WSH,
        though the way you do it depends on which platforms you set the
        variable, and whether you want to set it permanently.

        var System=Script.C reateObject("WS cript.Shell").E nvironment("SYS TEM");
        System("foo")=" bar";
        WScript.Echo(Sy stem("foo"));

        ....should work on win2k/XP and make the variable available to other
        processes/users. Using "USER" instead of "SYSTEM" makes it available to
        the current user only, and using "PROCESS" makes it available to the
        current process only.

        Windows 95/98 only support "PROCESS", so you cannot make the variable
        permanent unless using the "winset" utility.


        HTH
        Yep.

        Comment

        Working...