Question

  • Creator
    Topic
  • #2204399

    How to use username variable in a vbs script for insertion into a file path

    Locked

    by dummy.address ·

    below is a snippet of some vbs code that I am using to try and map drives. When I run the script below, it says it cannot find the path for T & W.

    I think it is because I am not using the variable I declared correctly. How can I make it so that when the script runs, the user’s username is inserted in the file path?

    Option Explicit

    Dim objSysInfo, objNetwork, strUserPath, objUser
    Dim adoCommand, adoConnection, strBase, strAttributes
    Dim objGroupList, oshell, WshSysenv, sUserName

    Set objSysInfo = CreateObject(“ADSystemInfo”)
    Set objNetwork = CreateObject(“Wscript.Network”)

    strUserPath = “LDAP://” & objSysInfo.UserName
    Set objUser = GetObject(strUserPath)
    Set oshell = CreateObject(“Wscript.shell”)
    Set WshSysEnv = oshell.Environment(“User”)
    sUsername = objnetwork.UserName

    ‘Removes curent mappings
    On Error Resume Next
    objNetwork.RemoveNetworkDrive “L:”, True, True
    objNetwork.RemoveNetworkDrive “J:”, True, True
    objNetwork.RemoveNetworkDrive “Z:”, True, True
    objNetwork.RemoveNetworkDrive “R:”, True, True
    objNetwork.RemoveNetworkDrive “N:”, True, True
    objNetwork.RemoveNetworkDrive “O:”, True, True
    objNetwork.RemoveNetworkDrive “U:”, True, True
    objNetwork.RemoveNetworkDrive “S:”, True, True
    objNetwork.RemoveNetworkDrive “M:”, True, True
    objNetwork.RemoveNetworkDrive “I:”, True, True
    objNetwork.RemoveNetworkDrive “K:”, True, True
    objNetwork.RemoveNetworkDrive “X:”, True, True
    objNetwork.RemoveNetworkDrive “Y:”, True, True
    objNetwork.RemoveNetworkDrive “Q:”, True, True
    objNetwork.RemoveNetworkDrive “P:”, True, True
    objNetwork.RemoveNetworkDrive “V:”, True, True
    On Error GoTo 0
    Wscript.Sleep(5000)

    objnetwork.mapnetworkDrive “T:”, “\\server.share.edu\com\profiles\'” & sUsername & “‘\My Documents”, False
    objnetwork.mapnetworkDrive “W:”, “\\server.share.edu\'” & sUsername & “‘”, False

All Answers

  • Author
    Replies
    • #2951245

      Clarifications

      by dummy.address ·

      In reply to How to use username variable in a vbs script for insertion into a file path

      Clarifications

    • #2952117

      The number of quotes looks a bit strange

      by neilb@uk ·

      In reply to How to use username variable in a vbs script for insertion into a file path

      I had to take off my glasses and polish them to see if I was getting double vision. 🙂

      You’ve got:

      objnetwork.mapnetworkDrive “W:”, “\\server.share.edu\'” & sUsername & “‘”, False
      objnetwork.mapnetworkDrive “T:”, “\\server.share.edu\com\profiles\'” & sUsername & “‘\My Documents”, False

      I would put

      objnetwork.mapnetworkDrive “W:”, “\\server.share.edu\” & sUsername & “, False
      objnetwork.mapnetworkDrive “T:”, “\\server.share.edu\com\profiles\” & sUsername & “\My Documents”, False

      Take out the single quotes as they aren’t needed. Then it should work fine.

      When you’re concatenating strings, it’s always worth replacing the command with a simple wscript.echo and then you can see what you’re passing to the command.

      🙂

    • #2952115

      Do you need those single quotes?

      by master of reality ·

      In reply to How to use username variable in a vbs script for insertion into a file path

      The sUsername seems ok, it works up on my machine using your code.
      If I run your script, the username is inserted into the paths:
      “\\server.share.edu\com\profiles\’MYUSERNAME’\My Documents” so I don’t think that’s your problem.
      Have you output the paths and tried mapping them manually to ensure that they are correct?

      • #2952094

        The single quotes are what is screwing it up

        by neilb@uk ·

        In reply to Do you need those single quotes?

        They are getting inserted in the path as the single quote is a perfectly acceptable file/path character – albeit one only used by people that I subsequently wish to kill.

        🙂

Viewing 2 reply threads