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!

%username% variable not working in vbs script 1

Status
Not open for further replies.

andyds

IS-IT--Management
Feb 22, 2007
91
GB
I am trying to copy a file from a public share to path on the current users local profile C:\Users\%UserName%\Documents. I have the script below working when I manually put in the users username. However, I want to make the script global and use the current username of whichever user may be running the script at the time. The %username% variable does not work. Here is what I have so far. Please help if you can.

Dim ObjFso
Dim StrSourceLocation
Dim StrDestinationLocation
Dim StrSourceFileName
Dim StrDestinationFileName
StrSourceLocation = "\\servershare\path\path"
StrDestinationLocation = "C:\Users\%username%\Documents"
StrSourceFileName = "file.dsn"
StrDestinationFileName = "file.dsn"
'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Copying the file
ObjFso.CopyFile StrSourceLocation & "\" & StrSourceFileName, StrDestinationLocation & "\" & StrDestinationFileName


 
VBScript does not expand environment variables, as you have discovered. However the scipting host Shell interface does ...

StrDestinationLocation = "C:\Users\" & createobject("wscript.shell").expandenvironmentstrings("%username%") & "\Documents"
 
Thanks for the reply. I put your line of code for what I had but it still does not work and fails on line 13.
 
My bad, it did work, had to change UNC path as file had been put somewher else!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top