Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

  1. username
  2. .\username
  3. localcomputername\username
  4. remotedomain.com\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 = "remotedomain.com", usernme = "username"
share|improve this question
add comment

1 Answer 1

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", _
    "remotedomain.com\username")

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

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

dim localhost, testdomain, outputDomain

localhost = "192.168.0.1"
for each testDomain in array( _
    ".", _
    localhost, _
    "", _
    "remotedomain.com")

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

    msgbox testDomain & " becomes " & outputDomain
Next

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.