Solved

Using Username in a Path in a VBS Script

Posted on 2010-11-24
6
567 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:gthmpd
  • 3
  • 3
6 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 34209860
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
 

Author Comment

by:gthmpd
ID: 34209941
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 34209950
Sure.

~BP
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:gthmpd
ID: 34304314
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
 
LVL 53

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 34323736
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
 

Author Closing Comment

by:gthmpd
ID: 34429782
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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question