Building paths with environment variables 
Author Message
 Building paths with environment variables

Building paths with environment variablesHere I use a common Sub that obtains these types of common things I use in scripts, mix and match as you need. The key is ExpandEnvironmentStrings from the WSHShell Object for your individual variables.

Shawn

Option Explicit

Dim strSystemRoot
Dim strCompName
Dim strWinDir
DIm strTempDir
Dim s

GetEnvironment

s = "%SystemRoot% = " & strSystemRoot & VBCRLF
s = s & "Computer Name = " & strCompName & VBCRLF
s = s & "%WinDir% = " & strWinDir & VBCRLF
s = s & "%Temp% = " & strTempDir

Wscript.Echo s
Wscript.Quit

Sub GetEnvironment()
 Dim oWSHShell
 Dim oWSHNetwork
 Set oWSHNetwork = WScript.CreateObject("WScript.Network")
 Set oWSHShell = Wscript.CreateObject("Wscript.Shell")

 strSystemRoot = oWshShell.ExpandEnvironmentStrings("%SystemRoot%")
 strCompName = oWshNetwork.ComputerName
 strWinDir = oWshShell.ExpandEnvironmentStrings("%windir%")
 strTempDir = oWshShell.ExpandEnvironmentStrings("%TEMP%")
 Set oWSHShell = Nothing
 Set oWSHNetwork = Nothing
End Sub


  I have written several log-in scripts at work in dos and I need to redo them with Windows Script Host. The problem is that I need to access things in a person's user directory and I don't see any way to do this with WSH. For instance...

  in dos I can do this:

  xcopy %windir%\Profiles\%username% y:

  How do I do this in Windows Script Host? I can't seem to build a path using Environment varibles like this in WSH. It's very frustrating.

  Please Help



Thu, 19 Sep 2002 04:00:00 GMT  
 Building paths with environment variables

Building paths with environment variablesSee if the WScript.Shell object's SpecialFolders property will give you what you want...

SpecialFolders Property
http://msdn.microsoft.com/scripting/windowshost/doc/wsProSpecialFolde...

--
Michael Harris
MVP Scripting

I have written several log-in scripts at work in dos and I need to redo them with Windows Script Host. The problem is that I need to access things in a person's user directory and I don't see any way to do this with WSH. For instance...

in dos I can do this:

xcopy %windir%\Profiles\%username% y:

How do I do this in Windows Script Host? I can't seem to build a path using Environment varibles like this in WSH. It's very frustrating.

Please Help



Thu, 19 Sep 2002 04:00:00 GMT  
 Building paths with environment variables

Building paths with environment variables
In JScript:

var shell = new ActiveXObject("WScript.Shell");
var cmd = shell.ExpandEnvironmentStrings("copy y:\\signatures\\*.* \"%windir%\\profiles\\%username%\\Application Data\\Microsoft\"");

In VBScript:

Dim Shell: Set Shell = CreateObject("WScript.Shell")
Dim Cmd: Cmd = Shell.ExpandEnvironmentStrings("copy y:\signatures\*.* ""%windir%\profiles\%username%\Application Data\Microsoft""")

Mike Whalen
Windows Script Dev

  I am trying to write a login script that copies files from a person's user directory on the network to their local profile directory on their c: drive. How do you do this in Windows Script Host? I can't seem to build a path using environment variables.

  For instance in dos I could write:

  copy y:\signatures\*.* %windir%\profiles\%username%\Application Data\Microsoft

  How can I mimic this statement with Windows Script Host?



Sat, 21 Sep 2002 03:00:00 GMT  
 Building paths with environment variables
Dim WshNetwork, WshShell, FileCopy, strUser, strWinDir, strSource, strDestination

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject( "WScript.Shell" )
Set FileCopy = CreateObject("Scripting.FileSystemObject")

strUser = WshNetwork.UserName
strWinDir = WshShell.ExpandEnvironmentStrings("%WINDIR%")

strDestination = strWindir & "\profiles\" & strUser & "\Application Data\Microsoft\"

'These lines copy all files in y:\signatures
strSource = "y:\signatures\*.*"
FileCopy.CopyFile strSource, strDestination,TRUE

'commented out for example
'These lines copy all files and subfolders in y:\signatures
'strSource = "y:\signatures\*"
'FileCopy.CopyFolder strSource, strDestination,TRUE

--
Regards,
Doug Knox, Microsoft MVPDTS
No support provided by e-mail!  Reply to the newsgroups only!
* Help us help YOU - http://members.home.com/dts-l/goodpost.htm
--
The Microsoft MVP Program does not constitute employment or contractual
obligation with Microsoft Corporation. We do this for fun! :)
--
Visit my web site for Win95/98 Tweaks, Tips and Utilities
http://members.xoom.com/dbknox/registry/index.htm


Building paths with environment variables
In JScript:

var shell = new ActiveXObject("WScript.Shell");
var cmd = shell.ExpandEnvironmentStrings("copy y:\\signatures\\*.* \"%windir%\\profiles\\%username%\\Application Data\\Microsoft\"");

In VBScript:

Dim Shell: Set Shell = CreateObject("WScript.Shell")
Dim Cmd: Cmd = Shell.ExpandEnvironmentStrings("copy y:\signatures\*.* ""%windir%\profiles\%username%\Application Data\Microsoft""")

Mike Whalen
Windows Script Dev

  I am trying to write a login script that copies files from a person's user directory on the network to their local profile directory on their c: drive. How do you do this in Windows Script Host? I can't seem to build a path using environment variables.

  For instance in dos I could write:

  copy y:\signatures\*.* %windir%\profiles\%username%\Application Data\Microsoft

  How can I mimic this statement with Windows Script Host?



Sat, 21 Sep 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Expanding SYSTEM %PATH% with embedded SYSTEM environment variable problem

2. cscript and the PATH-environment variable

3. Environment variable PATH

4. Error in changing the Path Environment variable

5. Increase length of system Path environment variable?

6. set environment variable path

7. environment variables: wshshell.environment or wshshell.expandenv ironmentstrings?

8. manipulate Environment Path

9. Set path environment

10. how to set PATH (environment) using WSH?

11. Building Variables on the Fly

12. Creating and environment variable and using it in the same cmd window

 

 
Powered by phpBB® Forum Software