Avatar of Member_2_7981047
Member_2_7981047
 asked on

If not exist vbs error

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

VB Script

Avatar of undefined
Last Comment
Member_2_7981047

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

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
ste5an

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

Member_2_7981047

ASKER
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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy