Solved

Using Username in a Path in a VBS Script

Posted on 2010-11-24
6
570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 54

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 54

Expert Comment

by:Bill Prew
ID: 34209950
Sure.

~BP
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 54

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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