Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with %USERPROFILE%

Status
Not open for further replies.

DougInCanada

Technical User
Feb 1, 2004
98
CA
I am trying to use the USERPROFILE environment variable in my script to create a specific file on a server.

strRequestor = objShell.ExpandEnvironmentStrings("%userprofile%")
strResponseFile = "\\ott-protect\Results\" & strRequestor & ".UAT"

Unfortunately, the userprofile is not cooperating. If the username is 8 characters, there's not a problem. But if the username is less than 8 characters (ie: 7) then strRequestor comes back as '\[username]'. The problem is also occurring with usernames less than 7 (ie: 5).

Regardless of the number of characters in the username, should this variable only come back with whatever follows [domain\] ?

If this is normal behaviour, how do I compensate for this in my code?

All help is greatly appreciated!
 
How is your objShell object defined. This code works on my Windows 7 machine. My username is 7 characters long
Code:
set objShell = CreateObject("WScript.Shell")
strRequestor = objShell.ExpandEnvironmentStrings("%userprofile%")
msgbox strRequestor

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
My objShell is set as follows:

Set objShell = CreateObject("WScript.Shell")

I'm running this on a Windows XP machine. I'm also running it on a domain, so my user accounts are all prefaced by the domain name (ie DomainName\UserName).

I can live with a work-around if it can read the userprofile string until it hits '\' then return any characters that follow as another string.
 
As a side note, this is a VB Script contained in an HTA, so that may be what's causing the issue and I'll need to use a work-around anyway...
 
My Bad!!!

the USERPROFILE is actually returning "C:\documents and Settings\[username]".

All I'm interested in is [username].

I'm trying to use the InstrRev function to locate the first '\', but that's not working correctly; it's treating instrRev like InStr and not reading the string from right to left, but from left to right.

HELP!
 
Code:
set objShell = CreateObject("WScript.Shell")
strRequestor = objShell.ExpandEnvironmentStrings("%userprofile%")
pos = InStrRev(strRequestor, "\")
strRequestor = Right(strRequestor, len(strRequestor) - pos)
msgbox strRequestor
 
Why not simply this ?
strRequestor = objShell.ExpandEnvironmentStrings("%username%")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
if all you want is the username, you could also user objNetwork.UserName

Code:
set objNetwork = CreateObject("WScript.Network")
strUsername = objNetwork.UserName

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top