Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

use environment variables in vbs script

Posted on 2011-02-18
6
Medium Priority
?
1,177 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:Tracy
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:
Tracy 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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:Tracy
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
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…

597 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