File Copy Script

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 16 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

File Copy Script

Post by mmacdonald »

Hello:

I would like to copy a file from a location to %SystemRoot%. I am able to locate the variable %SystemRoot% but I am unsure how to make the file copy.

Here's what I have:

'Option Explicit 'On Error Resume Next
Const OverwriteExisting = True Dim strDC, strComputer, strUser, objNetwork, objGroup, objDomain, objFSO, objUser

Set objNetwork = CreateObject("Wscript.Network") strComputer = objNetwork.ComputerName Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")Set WshSysEnv = WshShell.Environment("SYSTEM")
WScript.Echo WshSysEnv("WINDIR")

objFSO.CopyFile "C:test.txt" , "WshSysEnv("WINDIR")", OverwriteExisting

The last line is what I am not sure about.

Any suggestions would be appreciated.

User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

File Copy Script

Post by mmacdonald »

Hello:

I would like to copy a file from a location to %SystemRoot%. I am able to locate the variable %SystemRoot% but I am unsure how to make the file copy.

Here's what I have:

'Option Explicit 'On Error Resume Next
Const OverwriteExisting = True Dim strDC, strComputer, strUser, objNetwork, objGroup, objDomain, objFSO, objUser

Set objNetwork = CreateObject("Wscript.Network") strComputer = objNetwork.ComputerName Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")Set WshSysEnv = WshShell.Environment("SYSTEM")
WScript.Echo WshSysEnv("WINDIR")

objFSO.CopyFile "C:test.txt" , "WshSysEnv("WINDIR")", OverwriteExisting

The last line is what I am not sure about.

Any suggestions would be appreciated.

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

File Copy Script

Post by jvierra »

Very close. Try this:

Set WshShell = WScript.CreateObject("WScript.Shell")sSysRoot = WshShell.ExpandEnvironmentStrings("%WinDir%")
objFSO.CopyFile "C:test.txt" , sSysRoot, OverwriteExisting
User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

File Copy Script

Post by mmacdonald »

These did not work or more to the point, I am not sure how to make them work. I was not able to get the value of sSysRoot to echo to to the screen.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

File Copy Script

Post by jvierra »


This exact script works on every machine I have tried it on:

Set WshShell = WScript.CreateObject("WScript.Shell")sSysRoot = WshShell.ExpandEnvironmentStrings("%WinDir%")WScript.Echo sSysRoot

What OS are you using?

What error message are you getting?

YOur original script had many errors in it.

This:

objFSO.CopyFile "C:test.txt" , "WshSysEnv("WINDIR")", OverwriteExisting
won't work. It has too many quotes. It will also only return unreplaced values.

If you are on Windows 2000 then you probably need to upgrade teh script subsystem.

jvierra2008-03-15 23:08:05
User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

File Copy Script

Post by mmacdonald »

I tried again and it did echo to the screen and showed the correct value. My apologies. I get the error 'access denied' when I execute:

objFSO.CopyFile "C:test.txt" , sSysRoot, OverwriteExisting

This topic is 16 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked