Using Username in a Path in a VBS Script

As part of our VBS login script, we use it  to copy templates from a network share to the local machine.  The code compares the date of the file on the local machine to the file on the network, if it's older, it copies down the current one.  The script uses/reads a text file to determine which documents to copy.   Currently the files are copied to a static location in c:\program files.   What I'm trying to do is copy the templates to each user's profile.  What I can't figure out is how to impliment the code so it will enter the current user into the path.

The current code that does the copying looks like this:
t="C:\program files\"&lines(i)
              (the rest of the path is contained in the text files called by &lines)

What I would like to be able to do:

t="C:\Users\%username%\"&lines(i)

I know that you can't use environ variables in VBS, I put that in there as an example.

I'm using the following code to set the variable, though Im not positive it's correct either.

Set Network = CreateObject("WScript.Network")
CurrentUser = Network.UserName

I can provide more code if needed,  

Thanks
gthmpdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
You are there, just do the following to concatenate the username to the rest of the needed path:

Set Network = CreateObject("WScript.Network")
CurrentUser = Network.UserName
t = "C:\Users\" & CurrentUser & "\" & lines(i)

Open in new window

~bp
0
gthmpdAuthor Commented:
Thanks Bill for the prompt reply!


No Errors this time, but not the results I was looking for.  IE the file didnt copy down.   Would it be possible to send you my Script to look at?
0
Bill PrewCommented:
Sure.

~BP
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

gthmpdAuthor Commented:
Hey Bill,

I've attached the code i'm trying to modify.  In the 1st part is the code we used to copy templates from the network to a Win Xp computer. In the Program Files\Microsoft Office\ Folder.

What I want to do now is copy those same templates to a Win7 users profile.  (2nd Part)

I've highlighted the parts i'm focusing on,  the old code in green.  The new code is in yellow. I know that in the new code, where it sets the permissions is messed up, not sure how to do that either.

Thanks for taking a look.

JS
Copy-of-RASScript64.docx
0
Bill PrewCommented:
See if these changes make sense.  In addition, I think we can simplify the data compare logic a bit, but we can follow up on that if these changes get close.

In Win7, there is an environment variable called APPDATA that points to the users appdata location.  Do SET APPDATA at a command prompt to see what it contains.  In the VBS we reference that variable to get the users path to their appdata folder, this is the safest approach.

Naturally I couldn't test this, so check it over manually first and understand the changes I made to make sure they make sense.  Then do some actual testing with it.

'**THIS WILL SET THE ATTRIBUTES TO THE TEMPLATE FOLDERs SO NEW TEMPLATES CAN BE COPIED WITHOUT ERRORS**
   'Set oShell = CreateObject("Wscript.Shell")
   'sAppData = oShell.ExpandEnvironmentStrings("%APPDATA%")
	'WshShell.Run "attrib -R """ & sAppData & "\Microsoft\Templates\*.*"""
	'WshShell.Run "attrib -R """ & sAppData & "\Microsoft\Templates\GTH General\*.*"""
	'WshShell.Run "attrib -R """ & sAppData & "\Microsoft\Templates\gth litigation\*.*""" 
   'the line below is here for testing and debugging purposes
	'msgbox "The attributes on the template folders have been modified"

'**THIS CHECKS THE TEMPLATES ON THE USERS PC AND DOWNLOADS NEW FILES BASED ON MODIFIED DATE**
Function Update(source, target)
	Dim f1,f2,d1,d2,c1,c2
	set fs = Wscript.CreateObject ("Scripting.FileSystemObject")

	if fs.FileExists ( source ) then
	' source file is available
		set f1 = fs.GetFile ( source)
		d1 = f1.DateLastModified
		c1 = Year(d1) * 10000 + Month(d1) * 100 + Day(d1)
		If fs.FileExists ( target ) then
			set f2 = fs.GetFile (Target)
			d2 = f2.DateLastModified
			c2 = Year(d2) * 10000 + Month(d2) * 100 + Day (d2)
		else
			c2 = 0
		end if
		if c1 > c2 then
			'overwrite local copy with new version
			f1.Copy target,True
			WSH.Popup Message, 1,"File copy at work" 	
		end if
	end if
End Function

sub filetoarray()
	'Option Explicit
	'
	Dim fso, oFile,txt, lines, i, oShell, sAppData
   Set oShell = CreateObject("Wscript.Shell")
   sAppData = oShell.ExpandEnvironmentStrings("%APPDATA%")

	'' Create a FileSystemObject.
	'Create a text file listing all files to update
	Set fso = CreateObject("Scripting.FileSystemObject")

	'Path to source file
	Set oFile = fso.OpenTextFile("\\filenprint\Installs\SharePointDMSFiles\Templates\movetemplatesx64.txt")
'
	txt = oFile.ReadAll                  ' The rest of the file
	oFile.Close 

	'WScript.Echo txt     ' Show content.
	' Here we can try to process the content.
	' First, split all lines into an array.
	lines = Split(txt, vbCrLf)
	' Join the array data into one string and show it.
	'MsgBox Join(lines, vbCrLf)
	for i = 0 to 56 'number of items in list, -1
		'Insert paths here
		s = "\\filenprint\Installs\SharePointDMSFiles\templates\" & lines(i)
		t = sAppData & "\" & lines(i)
		update s, t
	next
end Sub

	dim WSH1
	Set WSH1 = CreateObject("WScript.Shell")
	filetoarray()

	'WSH.popup "Working . . . ", 10, "File Copy at Work."
	'msgbox "Copy Complete

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gthmpdAuthor Commented:
Thanks again, I had to alter the path for the move text file a little, but the code for the win7 info works fine.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.