Solved

Using Username in a Path in a VBS Script

Posted on 2010-11-24
6
568 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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