Solved

Use WshShell.Run to copy a file

Posted on 2007-11-21
4
5,163 Views
Last Modified: 2010-05-18
I'm tring to copy a file to 'All Users\Start Menu\Programs\Startup' folder.  I keep getting an error telling me it can't find the file specified.  The file is definitely in the 'd:\scripts' folder and it is definitely named 'JoinDomain.vbs'.

I'm sure the problem is syntax related, but I can't seem to find the answer.  Please help!

Thanks in advance.
Set oShell = CreateObject("Wscript.Shell")

strAllUser = oShell.ExpandEnvironmentStrings("%ALLUSERSPROFILE%")

DestAllStart = strAllUser & "\Start Menu\Programs\Startup"

ScriptPath = "d:\Scripts"

JDScript = ScriptPath & "\JoinDomain.vbs"

ShellCMD = "copy " & JDScript & " " & CHR(34) & DestAllStart & CHR(34) & " /Y"
 

Const OverwriteExisting = True

'Set objFSO = CreateObject("Scripting.FileSystemObject")

set WshShell = CreateObject("WScript.Shell")

WshShell.Run ShellCMD

Open in new window

0
Comment
Question by:VelocityNet
  • 2
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 20330764
Hi, try this....

'================
Set oShell = CreateObject("Wscript.Shell")
strAllUser = oShell.ExpandEnvironmentStrings("%ALLUSERSPROFILE%")
DestAllStart = strAllUser & "\Start Menu\Programs\Startup"
ScriptPath = "d:\Scripts"
JDScript = ScriptPath & "\JoinDomain.vbs"
ShellCMD = "cmd /c copy " & JDScript & " " & CHR(34) & DestAllStart & CHR(34) & " /Y"
 
Const OverwriteExisting = True
oShell.Run ShellCMD, 1, False
'================

You didn't need to set a second shell object, although that's not the issue, but I have added cmd /c to the ShellCMD line to force the copy command to run inside a command prompt, where we know it works.

You can also change
oShell.Run ShellCMD, 1, False

to
oShell.Run ShellCMD, 0, False

to hide the command prompt.

Regards,

Rob.
0
 

Author Comment

by:VelocityNet
ID: 20331238
Rob....It worked perfectly!

Thank you for your quick response!
0
 

Author Closing Comment

by:VelocityNet
ID: 31410448
Excellent work!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20331373
Great, no problem!

Regards,

Rob.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now