Search code examples
javascriptbatch-filevariablesvbscripthta

Pass remote Computer name variable from batch file to hta and print out the value in a pop up or html window


I'm trying to pass a variable from a batch file to an hta which needs to be displayed in a pop up window of some sort - either html or vbscript.

The computer name is looped through a batch file which opens individual instances so all I would need to pass is the variable "%1".

What I'm needing is a way to either bring that variable over to VBscript or Javascript, then print via html.


Solution

  • You can use CommandLine property of your HTA appliction object (i.e. its ID).

    It contains full verbatim command line with your HTA filename (and possibly path) followed by arguments you supplied when running the HTA

    This means you can pass arguments through the command line of your HTA, for example:

    start "" "c:\your-path\your-app.hta" "%~1"
    

    "%~1" ensures that the first parameter is always in double-quotes irregardless original %1 having them or not. This works only in an actual batch file.

    How to extract first commandline argument:

    • Check if first character of CommandLine is "
      • If so - find next "
      • Otherwise - find next space (" ") or tab ("\t")
    • Skip any consecutive spaces and/or TABs after position you found
    • Again check if first remaining character is "
      • If so - also find next " - your argument is between the double-quotes
      • Otherwise - your argument is between current position and first of: " ", "\t"` or end-of-string

    All of this can be done manually or with the help of regular expressions

    Since you are the one who runs the HTA you could in theory exclude code that runs in absence of double-quotes by always including them on the command line, although this is not recommended

    For more info see this article about passing parameters to HTA, it also includes example VBS code