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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

%username% 3

Status
Not open for further replies.

802IS

IS-IT--Management
Aug 12, 2008
6
US
I want to run a script based of the username;
in DOS I can use %username% to get the current user. how is that done is vbs?

ex:
C:\Documents and Settings\%username%\
 
Hope this helps

Code:
Dim WshShell, WshEnv, objFSO, WshNetwork
Const OverwriteExisting = True

'Configures Variables
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("Process")
Set WshNetwork = CreateObject("WScript.Network")

Set objSysInfo = CreateObject("ADSystemInfo")
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")

'Grab the user name
UserString = WshNetwork.UserName
Set objCurrentUser = GetObject("LDAP://" & objSysInfo.UserName)
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)

strFullName = objUser.FullName
strFirstName = objUser.FirstName


wscript.echo "Full Name " & strFullName
wscript.echo "First Name " & strFirstName
wscript.echo "strUser " & strUser
wscript.echo "Username " & UserString
 
Ok I used:

' objects needed to aquire username
Set objNetwork = CreateObject("WScript.Network")
Set objUser = CreateObject("ADSystemInfo")
Set UserName = GetObject("LDAP://" & objUser.UserName)

strDirDes = "C:\Documents and Settings\" & objNetwork.UserName & "\Application Data\Microsoft\Templates"
strDir = "\\file1Serv\install$\templates"



arrFile = array(strDirDes & "\CHCB Letterhead.doc", strDirDes & "\CHCB Memo Template.doc", strDirDes & "\CHCB Policy & Procedure Template.doc"
strDirDes & "\CHCB Admin FAX Template.doc", strDirDes & "\CHCB Clinical FAX Template.doc")


that worked but when I was declaring my array I spanned two lines and I know their is an operator needed. Any ideas

Thanks
 
Are you trying to do something like this

Dim Wshshell:Set WshShell = CreateObject("Wscript.Shell")
Dim oFSO:Set oFSO = CreateObject("Scripting.FileSystemObject")
strUserName = WshShell.ExpandEnvironmentStrings("%username%")


strDirDes = "C:\Documents and Settings\" & strUserName & "\Application Data\Microsoft\Templates"
strDir = "\\file1Serv\install$\templates\*.doc"

objFSO.CopyFile strdir, strDirdes, overwrite = True
 
BTW I suck at arrays and Ubound statements. Mark or dm4ever have always helped me in creating arrays, so hopefully they get to read this post and help out.I think it will be along these lines.

SysDrive = WshEnv("SYSTEMDRIVE")
SysRoot = WshEnv("SYSTEMROOT")
strName = objNetwork.UserName


strDirDes = SysDrive & "\Documents and Settings\" & strName & "\Application Data\Microsoft\Templates"
strSource = "\\chcb6\install$\templates"


arrFiles = Array("CHCB Letterhead.doc", "Template.doc", "Policy & Procedure Template.doc", _
"CHCB Admin FAX Template.doc", "CHCB Clinical FAX Template.doc")


For i = 0 To UBound(arrFiles)
arrFiles(i).copyfile (strSource) , (strDirDes) , OverwriteExisting, true
next
 
I was declaring my array I spanned two lines
arrFile = Array(strDirDes & "\CHCB Letterhead.doc", strDirDes & "\CHCB Memo Template.doc", strDirDes & "\CHCB Policy & Procedure Template.doc"[!], _[/!]
strDirDes & "\CHCB Admin FAX Template.doc", strDirDes & "\CHCB Clinical FAX Template.doc")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top