Question

Locked

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

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

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

The number of quotes looks a bit strange

by neilb@uk In reply to How to use username varia ...

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.

:)

Collapse -

Do you need those single quotes?

by Master Of Reality In reply to How to use username varia ...

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?

Collapse -

The single quotes are what is screwing it up

by neilb@uk In reply to Do you need those single ...

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.

:)

Back to Networks Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums