Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VBScript simple installer framework

0.00/5 (No votes)
24 May 2007 1  
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!
'#######################

'# 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here