Question Locked

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

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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

4 replies

by Member 25348569

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.

:)

by master of reality

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?

by Member 25348569

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.

:)

This discussion is closed to new replies.