Solved

If not exist vbs error

Posted on 2016-10-24
4
27 Views
Last Modified: 2016-10-24
I need this program to check if something doesn't exist, then copy itself to that location, The copy part works fine, but I keep getting an error on launch: Object required "

Option Explicit

Dim objShell, strCmd, strTargetFolder, objFSO, strFile, strDirectory

'Copy to startup section =====
strDirectory = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"

strFile = strDirectory & "\wippre-alpha.vbs"

If Not objFSO.FileExists(strFile) Then

strTargetFolder = "C:\Users\Arion\Documents\Environment\Test"

Set objShell = CreateObject("WScript.Shell")

strCmd = "%comspec% /c copy """ & WScript.ScriptFullName & """ """ & strTargetFolder & """ /Y"
objShell.Run strCmd
End if

Open in new window

0
Comment
Question by:Arion Sejdia
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
ste5an earned 500 total points
ID: 41857117
Correct. You don't instanciate objFSO.

Set objFSO = CreateObject("Scripting.FileSystemObject")

Open in new window


See also Accessing Files with FileSystemObject.

E.g.

Option Explicit

Dim objShell, strCmd, strTargetFolder, objFSO, strFile, strDirectory

strDirectory = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"
strFile = strDirectory & "\wippre-alpha.vbs"
strTargetFolder = "C:\Users\Arion\Documents\Environment\Test"
strCmd = "%comspec% /c copy """ & WScript.ScriptFullName & """ """ & strTargetFolder & """ /Y"

Set objFSO = CreateObject("Scripting.FileSystemObject"
If Not objFSO.FileExists(strFile) Then
  Set objShell = CreateObject("WScript.Shell")  
  objShell.Run strCmd
  Set objShell = Nothing
End if 

Set objFSO = Nothing

Open in new window


btw, why don't you use File.Copy of FileSystemObject instead of the command line?
1
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41857155
Don't you want the file to end up in this folder, not the target folder?

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

Also, is the script that this code will be in named wippre-alpha.vbs, or something else?  If that is the name then no need to append it to strFile, you can just use the directory as the destination of the copy.

Copy can't be done by file system object because in the prior question it was stated that the running VBS script was the file that needed to be copied ("copy itself"), and you can't do that with file system object, it throws an access denied error.

~bp
1
 
LVL 32

Expert Comment

by:ste5an
ID: 41857188
hmm..

Capture.PNG
with

Option Explicit

WScript.Echo "CopyMe"
FileCopyIfNotExists "C:\Temp\Fldr1\CopyMe.vbs", "C:\Temp\Fldr2\I'm a Hero!.vbs"
WScript.Echo "Done."
WScript.Echo

Public Function FileCopyIfNotExists(ASource, ADestination)

  On Error Resume Next

  Dim fso, f

  Set fso = CreateObject("Scripting.FileSystemObject")
  If Not fso.FileExists(ADestination) Then
    Set f = fso.GetFile(ASource)
    f.Copy(ADestination)    
	If Err.Number <> 0 Then
	  WScript.Echo "D'oh: " & Err.Description
	End If
	
	Set f = Nothing
  End if 

  Set fso = Nothing

End Function

Open in new window

0
 

Author Comment

by:Arion Sejdia
ID: 41857193
Cleaned version:

Option Explicit

Dim objShell, strCmd, strTargetFolder, objFSO, strFile, 

'Copy to startup section =====

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FileExists(strFile) Then

strTargetFolder = "C:\Users\Arion\Documents\Environment\Test"

Set objShell = CreateObject("WScript.Shell")

strCmd = "%comspec% /c copy """ & WScript.ScriptFullName & """ """ & strTargetFolder & """ /Y"
objShell.Run strCmd
End if

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

16 Experts available now in Live!

Get 1:1 Help Now