Solved

If not exist vbs error

Posted on 2016-10-24
4
73 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

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 34

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

751 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