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
Solved

use environment variables in vbs script

Posted on 2011-02-18
6
1,156 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
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.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Outlook Free & Paid Tools
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

860 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