Solved

If not exist vbs error

Posted on 2016-10-24
4
56 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 33

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 54

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 33

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
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 …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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