vb script to install app after checking for a file

I am trying to put together a script bundled from different web sources which will checkfor the existance of a file and run an executable if it does NOT exist.

What this is doing is supposed to do is checking for the existance of a driver file in c:\program files and if it is not there running the exe.

I get an error and am not sure what.

' this script will check for the existance of a file and run an app if it is not found.

Option Explicit

Sub Main()

Dim LogFile
Dim TxtFile
Dim AppToRun
Dim ts
Dim Running

   ' These values are just for example and testing... substitute your own real values here.
   LogFile  = "C:\program files\cups driver\cups6.inf"
   TxtFile  = "C:\program files\cups driver\cups6.ini"
   AppToRun = "\\\Applications\Cups Driver\Cups driver discovery PDF.exe"

   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set WSH = CreateObject("Wscript.Shell")

   ' If target file doesn't exist, install app.
   If Not FSO.FileExists(TxtFile) Then
      Wscript.Echo "File does not exist."
   End If

   ' see if we need to create inf file...
   If FSO.FileExists(LogFile) Then
      Wscript.Echo "Aborting... application is installed."
   End If

   ' If we get this far then we can run the application...
   Wscript.Echo "File does exist."
   Set Running = WSH.Exec(AppToRun)

'End Sub

The exe in question installs the driver files
Who is Participating?
CetusMODConnect With a Mentor Commented:
PAQed with points refunded (260)

Community Support Moderator

Probably the simplest script to acheive that would be something like:

'Start of Script
Option Explicit

DIM txtFile
Dim AppToRun

TxtFile  = "C:\test.xxx"
AppToRun = "Notepad.exe"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("Wscript.Shell")

' If target file doesn't exist, install app.
IF NOT FSO.FileExists(TxtFile) THEN
   Wsh.Run AppToRun

SET FSO=Nothing   ' Clean everything up.
SET WSH=Nothing
SET txtFile=Nothing
SET AppToRun=Nothing
' End of Script

Copy the above and paste into a .VBS file - in this instance if there's no file called 'test.xxx' on the root of drive C, NotePad will be launched.

Hope that helps,
Sub FileExists()
Dim fso
Dim strRunProgram
Dim file As String
file = "C:\Test.xls" ' change to match the file w/Path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(file) Then
    MsgBox file & " was not located.", vbInformation, "File Not Found"
    strRunProgram = "c:\execute.exe" 'path to executable
    WshShell.Run strRunProgram, 1, True
    MsgBox file & " has been located.", vbInformation, "File Found"
End If
End Sub
sid20vtAuthor Commented:
I needed to make sure it would not run over and over at every startup/login.
So check for existance of a file and run the application.  If its already installed, dont run it again.

In the end I used kix script to get this done.  Seemed to be the easiest way to make it work.

Sorry fostejo and cschipper.  Did not try the vb solutions you sent in the end.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.