Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

If not exist vbs error

Posted on 2016-10-24
4
Medium Priority
?
162 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 36

Accepted Solution

by:
ste5an earned 2000 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 59

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 36

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

877 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