?
Solved

use environment variables in vbs script

Posted on 2011-02-18
6
Medium Priority
?
1,167 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
[X]
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
  • 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

764 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