Solved

use environment variables in vbs script

Posted on 2011-02-18
6
1,149 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

744 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

10 Experts available now in Live!

Get 1:1 Help Now