Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The user can input a string, so basically 4 options:

  1. username
  2. .\username
  3. localcomputername\username

What is the most robust way to return the domain and username given that input string in vbs?

So for the 4 input strings above, here are the outputs:

  1. domain = "localcomputername", usernme = "username"
  2. domain = "localcomputername", usernme = "username"
  3. domain = "localcomputername", usernme = "username"
  4. domain = "", usernme = "username"
share|improve this question
up vote 0 down vote accepted

I would do it in a two phases:
First split the input in a username and a password with a regular expression

Option Explicit

dim re, matches, match, test

Set re = new regexp
re.pattern = "^((.*)\\)?(.*)$"

for each test in array( _
    "username", _
    ".\username", _
    "localcomputername\username", _

    Set matches = re.Execute(test)
    for each match in matches
        msgbox "domain: " & match.submatches(1) & vbNewLine & _
                "username: " & match.submatches(2)

Then, Use a Select Case to make the domainname valid:

dim localhost, testdomain, outputDomain

localhost = ""
for each testDomain in array( _
    ".", _
    localhost, _
    "", _

    select case testDomain
        case localhost, "", "."
            outputDomain = localhost
        case else
            outputDomain = testDomain
    End Select

    msgbox testDomain & " becomes " & outputDomain

Combine these two and you have your script to get the domain and username.
Disclaimer: The above code is tested examplecode, but you have to adapt it to your specific situation.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .