Click here to Skip to main content
15,895,827 members
Articles / Programming Languages / VBScript
Article

VBScript simple installer framework

Rate me:
Please Sign up or sign in to vote.
4.71/5 (8 votes)
24 May 2007CPOL 45.2K   353   28   2
This script is designed to be a light-weight, flexible utility to aid in rolling out packages.

Introduction

This utility can be used for rolling out utilities and patches to end users, or to package an application for use in Windows systems. This installer framework supports all environment vars used in Windows XP, so it should be easy to code with compatibility in mind.

Background

Basically, you just include the names and locations of the files to be copied, whether or not each file will be linked or registered; then, cut some custom processing code. Simple.

Using the code

Edit this script:

  1. Enter the locations of the source files.
  2. Enter the destination paths for each file.
  3. Decide if you want a link or hotkey created.
  4. Decide if you need to register the file.
  5. Enter any custom procedures that are necessary (if any).
  6. Run it!
VBScript
'#######################

'# Installer Framework #

'#  v.1.0              #

'# Brian Velde         #

'# 2007-01-02          #

'#######################

Option Explicit
    
    Dim sh, fso, dict 'CREATE SHELL, FILESYSTEMOBJ, AND DICTIONARY

    Set sh = CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set dict = CreateObject("Scripting.Dictionary")

'# DEFINE PROGRAM INFORMATION #'

    Dim strProgramName, strProgramMajorVersion, _
        strProgramMinorVersion
    Dim strProgramBuild, strProgramAuthor, _
        strCreationDate, displayBuildNumber
    
    strProgramName = "<program_name>"
    strProgramMajorVersion = "v0"
    strProgramMinorVersion = "1"
    strProgramBuild = "1"
    strProgramAuthor = "Brian Velde"
    strCreationDate = "2007"
    displayBuildNumber = False
'# END DEFINE PROGRAM INFORMATION #'


'# SETUP ENVIRONMENT VARIABLES #'

    Dim evallusersprofile, evappdata, evcd, evcmdcmdline, 
        evcomputername, evcomspec, evdate 'ENVIRONMENT VARIABLES

    Dim everrorlevel, evhomedrive, evhomepath, evhomeshare, 
        evlogonserver, evnumber_of_processors 'ENVIRONMENT VARIABLES

    Dim evos, evpath, evpathext, evprocessor_architecture, 
        evprogramfiles, evprompt, evrandom 'ENVIRONMENT VARIABLES

    Dim evsystemdrive, evsystemroot, evtemp, evtmp, evtime, 
        evuserdomain, evusername, evuserprofile, evwindir 'ENVIRONMENT VARIABLES

    
    evallusersprofile = sh.ExpandEnvironmentStrings("%allusersprofile%")
    evappdata = sh.ExpandEnvironmentStrings("%appdata%")
    evcd = sh.ExpandEnvironmentStrings("%cd%")
    evcmdcmdline = sh.ExpandEnvironmentStrings("%cmdcmdline%")
    evcomputername = sh.ExpandEnvironmentStrings("%computername%")
    evcomspec = sh.ExpandEnvironmentStrings("%comspec%")
    evdate = sh.ExpandEnvironmentStrings("%date%")
    everrorlevel = sh.ExpandEnvironmentStrings("%errorlevel%")
    evhomedrive = sh.ExpandEnvironmentStrings("%homedrive%")
    evhomepath = sh.ExpandEnvironmentStrings("%homepath%")
    evhomeshare = sh.ExpandEnvironmentStrings("%homeshare%")
    evlogonserver = sh.ExpandEnvironmentStrings("%logonserver%")
    evnumber_of_processors = _
          sh.ExpandEnvironmentStrings("%number_of_processors%")
    evos = sh.ExpandEnvironmentStrings("%os%")
    evpath = sh.ExpandEnvironmentStrings("%path%")
    evpathext = sh.ExpandEnvironmentStrings("%pathext%")
    evprocessor_architecture = _
          sh.ExpandEnvironmentStrings("%processor_architecture%")
    evprogramfiles = sh.ExpandEnvironmentStrings("%programfiles%")
    evprompt = sh.ExpandEnvironmentStrings("%prompt%")
    evrandom = sh.ExpandEnvironmentStrings("%random%")
    evsystemdrive = sh.ExpandEnvironmentStrings("%systemdrive%")
    evsystemroot = sh.ExpandEnvironmentStrings("%systemroot%")
    evtemp = sh.ExpandEnvironmentStrings("%temp%")
    evtmp = sh.ExpandEnvironmentStrings("%tmp%")
    evtime = sh.ExpandEnvironmentStrings("%time%")
    evuserdomain = sh.ExpandEnvironmentStrings("%userdomain%")
    evusername = sh.ExpandEnvironmentStrings("%username%")
    evuserprofile = sh.ExpandEnvironmentStrings("%userprofile%")
    evwindir = sh.ExpandEnvironmentStrings("%windir%")
'# END SETUP ENVIRONMENT VARIABLES #'


'# DEFINE ENVIRONMENT INFORMATION #'

    Dim installPath
    Dim extraFolders(2)
    'SET TO THE NEMBER OF EXTRA FOLDERS YOU NEED. (2 default)

    
    installPath = "" 'PROGRAM INSTALL DIRECTORY.

    extraFolders(0) = "" 'PATH TO FOLDER TO CREATE... 

    extraFolders(1) = "" 'PATH TO FOLDER TO CREATE...

'# END DEFINE ENVIRONMENT INFORMATION #'


'# DEFINE FILES TO BE COPIED #'

    Dim fileSrc(10) 'CHANGE TO THE NUMBER OF FILES (min 10)

    Dim fileDst(10) 'CHANGE TO THE NUMBER OF FILES (min 10)

    Dim linkTarget(10) 'SAVE LINK TARGET

    Dim linkLocation(10) 'SAVE OUTPUT LINK LOCATION

    Dim regFile(10) 'REGISTER FILE AS LIBRARY (BOOL)

    Dim hotKey(10) '"ALT+CTRL+F"

    
    '###########################################################

    '#         YOU MUST INCLUDE FILENAME IN DST PATH!!           #

    '#    ( evsystemdrive & "\<folder_name>\<file_name>.<ext>" ) #

    '###########################################################

    
    fileSrc(0) = "" 'COPY THIS FILE

    fileDst(0) = "" 'TO THIS LOCATION

    linkTarget(0) = fileDst(0) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(0) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(0) = False 'REGISTER FILE AS LIBRARY

    hotKey(0) = "" '"ALT+CTRL+F"

    
    fileSrc(1) = "" 'COPY THIS FILE

    fileDst(1) = "" 'TO THIS LOCATION

    linkTarget(1) = fileDst(1) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(1) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(1) = False 'REGISTER FILE AS LIBRARY

    hotKey(1) = "" '"ALT+CTRL+F"


    fileSrc(2) = "" 'COPY THIS FILE

    fileDst(2) = "" 'TO THIS LOCATION

    linkTarget(2) = fileDst(2) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(2) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(2) = False 'REGISTER FILE AS LIBRARY

    hotKey(2) = "" '"ALT+CTRL+F"

    
    fileSrc(3) = "" 'COPY THIS FILE

    fileDst(3) = "" 'TO THIS LOCATION

    linkTarget(3) = fileDst(3) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(3) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(3) = False 'REGISTER FILE AS LIBRARY

    hotKey(3) = "" '"ALT+CTRL+F"

    
    fileSrc(4) = "" 'COPY THIS FILE 

    fileDst(4) = "" 'TO THIS LOCATION

    linkTarget(4) = fileDst(4) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(4) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(4) = False 'REGISTER FILE AS LIBRARY

    hotKey(4) = "" '"ALT+CTRL+F"


    fileSrc(5) = "" 'COPY THIS FILE 

    fileDst(5) = "" 'TO THIS LOCATION

    linkTarget(5) = fileDst(5) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(5) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(5) = False 'REGISTER FILE AS LIBRARY

    hotKey(5) = "" '"ALT+CTRL+F"

    
    fileSrc(6) = "" 'COPY THIS FILE 

    fileDst(6) = "" 'TO THIS LOCATION

    linkTarget(6) = fileDst(6) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(6) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(6) = False 'REGISTER FILE AS LIBRARY

    hotKey(6) = "" '"ALT+CTRL+F"

    
    fileSrc(7) = "" 'COPY THIS FILE 

    fileDst(7) = "" 'TO THIS LOCATION

    linkTarget(7) = fileDst(7) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(7) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(7) = False 'REGISTER FILE AS LIBRARY

    hotKey(7) = "" '"ALT+CTRL+F"


    fileSrc(8) = "" 'COPY THIS FILE 

    fileDst(8) = "" 'TO THIS LOCATION\

    linkTarget(8) = fileDst(8) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(8) = "" 'LOCATION OF OUTPUT LINK (blank = no link)

    regFile(8) = False 'REGISTER FILE AS LIBRARY

    hotKey(8) = "" '"ALT+CTRL+F"

    
    fileSrc(9) = "" 'COPY THIS FILE 

    fileDst(9) = "" 'TO THIS LOCATION

    linkTarget(9) = fileDst(9) 'SAVE LOCATION OF FILE FOR LINKING

    linkLocation(9) = "" 'LOCATION OF OUTPUT LINK ("" = no link)

    regFile(9) = False 'REGISTER FILE AS LIBRARY

    hotKey(9) = "" '"ALT+CTRL+F"

'# END DEFINE FILES TO BE COPIED #'


'# BEGIN INSTALLER FUNCTIONS #'

    Dim win98, win2k, winXP, win2k3, winVista 'getOsVer() (BOOLEAN)


    Function getOsVer()
        Dim SystemSet, system, osVer
        Set SystemSet = _
          GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
        For each System in SystemSet
            osVer = System.Caption
           
               '# DETERMINE IF OS IS VISTA

               If InStr(osVer, "Vista") Then
                   winVista = True
               Else
                   winVista = False
               End If
               
               '# DETERMINE IF OS IS 2003

               If InStr(osVer, "2003") Then
                 win2k3 = False
            Else
                win2k3 = True
            End If
            
            '# DETERMINE IF OS IS XP

            If InStr(osVer, "XP") Then
                winXP = True
            Else
                winXP = False
            End If
            
            '# DETERMINE IF OS IS 2000 PRO/SERVER

            If InStr(osVer, "2000") Then
                win2k = True
            Else
                win2k = False
            End If    
            
            '# DETERMINE IF OS IS win98

            If InStr(osVer, "98") Then
                win98 = True
            Else
                win98 = False
            End If 
        Next
    End Function    
    
    Function createDirs() 
        Dim max, i
        If Not fso.FolderExists(installPath) AND installPath <> "" Then
            fso.CreateFolder(installPath)
        End If
        i=0
        max = UBound(extraFolders)
        While (i < max)
            If Not fso.FolderExists(extraFolders(i)) And extraFolders(i) <> "" Then
                fso.CreateFolder(extraFolders(i))
            End If
            i = i+1
        Wend
    End Function
    
    Function copyFiles()
        Dim i, max
        i = 0
        max = UBound(fileSrc)
        While (i < max)
            If (fileSrc(i) <> "") Then
                fso.CopyFile fileSrc(i), fileDst(i), True
            End If
            i = i+1
        Wend 
    End Function 
    
    Function createShortcuts()
        Dim outputLink, oLink, i, max
        i = 0
        max = UBound(linkTarget)
        While (i < max)
            If (linkLocation(i) <> "") Then 
                outputLink = linkLocation(i)
                  Set oLink = sh.CreateShortcut(outputLink)
                oLink.TargetPath = linkTarget(i)
                '    oLink.Arguments = ""

                '    oLink.Description = "MyProgram"

                If hotKey(i) <> "" Then
                    oLink.HotKey = hotKey(i)
                End If
                oLink.IconLocation = linkTarget(i)
                '    oLink.WindowStyle = "1"

                '    oLink.WorkingDirectory = "C:\Program Files\MyApp"

                oLink.Save
                Set oLink = Nothing
            End If
            i = i+1
        Wend
    End Function 
    
    Function registerFiles()
        Dim result, i, command
        i=0
        While (i < UBound(fileDst))
            If regFile(i) <> False Then
                command = """" & evsystemroot & _
                          "\system32\regsvr32.exe""" & _
                          " """ & fileDst(i) & """"
                sh.Exec command
            End If
            i = i+1
        Wend                
    End Function
    
    Function cleanup()
        Set sh = Nothing
        Set fso = Nothing
        Set dict = Nothing
    End Function
'# END INSTALLER FUNCTIONS #'


'# PROMPT USER IF THEY WISH TO INSTALL #'

Dim installMsg, endMsg 'SETUP BEGIN AND END MESSAGES (OBJ)


If displayBuildNumber = True Then
    installMsg = MsgBox("Do you wish to install " & strProgramName &_
                        " " & strProgramMajorVersion & _
                        "." & strProgramMinorVersion & _
                        " build " & strProgramBuild & _
                        "?",vbYesNo,"Install " _
                        & strProgramName & "?")
Else
    installMsg = MsgBox("Do you wish to install " & _
                 strProgramName & " " & _
                 strProgramMajorVersion & "." & strProgramMinorVersion & _
                 "?",vbYesNo,"Install " & strProgramName & "?")
End If

If installMsg = vbNo Then
    WScript.Quit(666)
End If 

'# BEGIN INSTALL PROCEDURES #'

Call getOsVer()

Call createDirs()

Call copyFiles()

Call createShortcuts()

Call registerFiles()

'# BEGIN USER DEFINED INSTALL PROCEDURES #'

'# END USER DEFINED INSTALL PROCEDURES #'


Call cleanup()
'# END INSTALL PROCEDURES #'    


'# DISPLAY FINISHED MESSAGE #'

endMsg = MsgBox("Installation of " & strProgramName & _
         " " & strProgramMajorVersion & "." & _
         strProgramMinorVersion & _
         " has completed successfully!", vbOKOnly, "Finished!")

WScript.Quit(0)

Points of interest

This script saves me a lot of time... Nuf said...

History

  • Version 1.1 - Uploaded 5-24-2007.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalreally cool script Pin
thompsonson228-May-07 22:20
thompsonson228-May-07 22:20 
GeneralRe: really cool script Pin
xExTxCx10-Jun-07 9:31
xExTxCx10-Jun-07 9:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.