Solved

Using Username in a Path in a VBS Script

Posted on 2010-11-24
6
559 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 51

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 51

Expert Comment

by:Bill Prew
ID: 34209950
Sure.

~BP
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 51

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now