Solved

use environment variables in vbs script

Posted on 2011-02-18
6
1,151 Views
Last Modified: 2012-05-11
I have the following VB script:-

-----------------------------------------------------------------
Dim objFso
Dim strSource
Dim strDestination
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
strSource = "\\MYDOMAINCONTROLLER\netlogon\Office_2007_Templates\PowerPoint"
strDestination = "C:\Documents and Settings\MYUSERNAME\Application Data\Microsoft\Templates"

if objFso.FolderExists(strSource) = true then

if objFso.FolderExists(strDestination) = true then
set objFolder = objFso.GetFolder(strSource)
Set objFiles = objFolder.Files

for each objFile in objFiles

If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
If objFso.FileExists(strDestination & objFile.Name) = False Then objFSO.CopyFile objFile.Path, strDestination, False
      
next
end if

end if
--------------------------------------------------------------------------------


But I need the script to use environment variables instead of the fully qualified path for the file copy so that it can be used against any user etc.


"\\MYDOMAINCONTROLLER\netlogon\Office_2007_Templates\PowerPoint"
I need it to use:-
%logonserver%\netlogon\Office_2007_Templates\PowerPoint


"C:\Documents and Settings\MYUSERNAME\Application Data\Microsoft\Templates"
I need it to use:-
%APPDATA%\Microsoft\Templates


Please advise.....Thanks
0
Comment
Question by:browndobe
  • 3
  • 3
6 Comments
 
LVL 24

Expert Comment

by:broomee9
ID: 34927719
Try something like this:

Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo user & " " & comp

http://www.windowsitpro.com/article/tips/jsi-tip-9956-how-do-i-use-an-environment-variable-in-vbscript-.aspx
0
 

Author Comment

by:browndobe
ID: 34927779
I saw that article...  Where do I place that in the script?
0
 
LVL 24

Accepted Solution

by:
broomee9 earned 50 total points
ID: 34927825
Try something like this:
Dim objFso
Dim strSource
Dim strDestination
Dim oShell

Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject( "WScript.Shell" )
strSource = oShell.ExpandEnvironmentStrings("%logonserver%")  & "\netlogon\Office_2007_Templates\PowerPoint"
strDestination = oShell.ExpandEnvironmentStrings("%UserName%") & "\Microsoft\Templates"

'Check the strings
WScript.Echo strSrouce & " " & strDestination

if objFso.FolderExists(strSource) = true then

if objFso.FolderExists(strDestination) = true then
set objFolder = objFso.GetFolder(strSource)
Set objFiles = objFolder.Files

for each objFile in objFiles

If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
If objFso.FileExists(strDestination & objFile.Name) = False Then objFSO.CopyFile objFile.Path, strDestination, False
      
next
end if

end if

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:browndobe
ID: 34928050
thanks broomee9,  

the script works but now it displays a popup on the desktop displaying the:-  %APPDATA%\Microsoft\Templates
Path.
0
 
LVL 24

Expert Comment

by:broomee9
ID: 34928062
Yes, that's what this does:

'Check the strings
WScript.Echo strSrouce & " " & strDestination

Just remove it, I added it in so that you could see the path is correct.

0
 

Author Comment

by:browndobe
ID: 34928228
broomee9, you are a gentleman and a scholar...

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show you how to use shortcut menus in the Access run-time environment.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

867 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

19 Experts available now in Live!

Get 1:1 Help Now