Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need %systemroot% help

Status
Not open for further replies.

GCFII

IS-IT--Management
May 17, 2004
20
US
I am trying to copy a file from \\servername\folder\myfile.txt to c:\windows in some cases and c:\winnt in other cases. Is there a way to make this work using %systemroot% in a vbs script instead of C:\Windows\?

What I have so far is:

Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "\\servername\folder\myfile.txt" , "C:\Windows\", OverwriteExisting

This works but I need to be able to use %systemroot% in place of C:\Windows\ so it will work arcross various OS platforms.

Thx
 
Try using:

ExpandEnvironmentStrings Method
See Also
WshShell Object
Language
JScript

VBScript

Show All
Returns an environment variable's expanded value.

object.ExpandEnvironmentStrings(strString)
Arguments
object
WshShell object.
strString
String value indicating the name of the environment variable you want to expand.
Remarks
The ExpandEnvironmentStrings method expands environment variables defined in the PROCESS environment space only. Environment variable names, which must be enclosed between "%" characters, are not case-sensitive.

Example
The following code expands the Windows Directory environment variable and displays it:

[VBScript]
set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%WinDir%")
[JScript]
var WshShell = WScript.CreateObject("WScript.Shell");
WScript.Echo("WinDir is " + WshShell.ExpandEnvironmentStrings("%WinDir%"));
See Also
WshShell Object


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks but I saw this in the MSDN library and I am still at a loss as to how to make it work with what I am trying to do. I am a bit of a newbie in the scripting world but I just assumed that this was something that would have been attempted before. Would it be possible to post an example of a file being copied from one location to %systemroot% or %windir%, that would help a ton.

Thanks
 
Hello GCFII,

This may show how it glues together.
[tt]
sdestpath=createobject("wscript.shell").expandenvironmentstrings("%systemroot%")
objFSO.CopyFile "\\servername\folder\myfile.txt",sdestpath & "\",true
[/tt]
Of course, systemroot works for winnt series.

regards - tsuji
 
Thank You tsuji!!

This works for both C:\Windows and C:\WINNT. This is exaclty what I needed. B-)

THX again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top