vb script to install app after checking for a file

Posted on 2006-06-08
Last Modified: 2008-05-13
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
Question by:sid20vt
    LVL 10

    Expert Comment


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

    'Start of Script
    Option Explicit

    Dim FSO
    Dim WSH
    DIM txtFile
    Dim AppToRun

    TxtFile  = "C:\"
    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
    END IF

    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 '' on the root of drive C, NotePad will be launched.

    Hope that helps,
    LVL 2

    Expert Comment

    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

    Author Comment

    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.

    Accepted Solution

    PAQed with points refunded (260)

    Community Support Moderator

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now