17 October 2012

Font Installation Script

This script will install all fonts residing in the same folder as this script resides. I reads all of the font file names in the directory into an array. It then binds the script to the fonts folder and copies the fonts over to it, thereby initiating an automatic install upon copying. As an error checking mechanism, it first checks to make sure the font does not already exists in the c:\windows\Fonts folder. If it does, it skips over the installation of that font.

NOTE: On line 81, I have it compare the name in the array to the hardcoded filename. If they are the same, then it skips and goes to the next one. This is so the script does not read the VB Script. I left off the extension because I encapsulate the VB Script within an IPF file so that SCCM only has to execute the EXE. By leaving off the extension, it doesn't read the install.ipf, install.vbs, install.exe, or the install.wsm files.

Here is the download.

 '*******************************************************************************  
 '   Author: Mick Pletcher  
 '    Date: 17 October 2012  
 '  Modified:  
 '  
 ' Description: This will install all fonts residing in the same folder as this  
 '                 script.  
 '                 1) Define the relative installation path  
 '                 2) Create the Log Folder  
 '                 3) Read list of Fonts into Array  
 '                 4) Install Fonts  
 '                 5) Cleanup Global Memory  
 '*******************************************************************************  
 Option Explicit  

 REM Define Constants  
 CONST TempFolder    = "c:\temp\"  
 CONST LogFolderName = "Fonts"  

 REM Define Global Variables  
 DIM Count        : Count = 1  
 DIM LogFolder    : LogFolder = TempFolder & LogFolderName & "\"  
 DIM RelativePath : Set RelativePath = Nothing  
 ReDIM arrFiles(1)  

 REM Define the relative installation path  
 DefineRelativePath()  
 REM Create the Log Folder  
 CreateLogFolder()  
 REM Read list of Fonts into Array  
 ReadFonts()  
 REM Install Fonts  
 InstallFonts()  
 REM Cleanup Global Memory  
 GlobalMemoryCleanup()  

 '*******************************************************************************  
 '*******************************************************************************  

 Sub DefineRelativePath()  

      REM Get File Name with full relative path  
      RelativePath = WScript.ScriptFullName  
      REM Remove file name, leaving relative path only  
      RelativePath = Left(RelativePath, InStrRev(RelativePath, "\"))  

 End Sub  

 '*******************************************************************************  

 Sub CreateLogFolder()  

      REM Define Local Objects  
      DIM FSO : Set FSO = CreateObject("Scripting.FileSystemObject")  

      If NOT FSO.FolderExists(TempFolder) then  
           FSO.CreateFolder(TempFolder)  
      End If  
      If NOT FSO.FolderExists(LogFolder) then  
           FSO.CreateFolder(LogFolder)  
      End If  

      REM Cleanup Local Variables  
      Set FSO = Nothing  

 End Sub  

 '*******************************************************************************  

 Sub ReadFonts()  

      REM Define Local Objects  
      DIM FSO : Set FSO = CreateObject("Scripting.FileSystemObject")  

      REM Define Local Variables  
      DIM Folder : Set Folder = FSO.GetFolder(RelativePath)  
      DIM Files  : Set files  = Folder.Files  
      DIM File   : Set File   = Nothing  

      For each File in Files  
           If NOT Left(File.Name,Len(File.Name)-4) = "Install" then  
                arrFiles(Count) = File.Name  
                Count = Count + 1  
                ReDim Preserve arrFiles(Count)  
           End If  
      Next  
      Count = Count - 1  

      REM Cleanup Local Memory  
      Set File   = Nothing  
      Set Files  = Nothing  
      Set Folder = Nothing  
      Set FSO    = Nothing  

 End Sub  

 '*******************************************************************************  

 Sub InstallFonts()  

      REM Define Local Constants  
      Const FONTS = &H14&  

      REM Define Local Objects  
      DIM FSO      : Set FSO = CreateObject("Scripting.FileSystemObject")  
      DIM i        : Set i = Nothing  
      DIM oShell   : SET oShell = CreateObject("Shell.Application")  
      DIM oFolder  : Set oFolder = oShell.Namespace(FONTS)  
      DIM WshShell : Set WshShell = WScript.CreateObject("Wscript.Shell")  

      For i = 1 to Count  
           If NOT FSO.FileExists("c:\windows\Fonts\" & arrFiles(i)) then  
                oFolder.CopyHere RelativePath & arrFiles(i), 16  
           End If  
      Next  

      REM Cleanup Local Variables  
      Set FSO = Nothing  
      Set i = Nothing  
      Set oFolder = Nothing  
      Set oShell = Nothing  
      Set WshShell = Nothing  

 End Sub  

 '*******************************************************************************  

 Sub GlobalMemoryCleanup()  

      Set Count = Nothing  
      Set LogFolder = Nothing  
      Set RelativePath = Nothing  
      Erase arrFiles  

 End Sub  

9 comments:

  1. This script works great except the fact that it is trying to read the .vbs file and errors out. How do I either tell the script to ignore the .vbs file or put the fonts in a separate folder?

    ReplyDelete
  2. On line 81, change the "Install" to whatever the name of the vbs file is. I have it compare the filename it is reading with the filename of the vb script. If you have kept the name of the vbs file the same, then just change "Install" to "InstallFonts". That should take care of that issue.

    ReplyDelete
  3. Wow, what a quick response, thanks! I feel stupid now after reading thoroughly through the script and seeing what you have done. Again, great work! I changed the name back to what you made it and it is working great.

    ReplyDelete
  4. You're welcome. The reason I do not have it read the filename of the VB Script is because I wrap an IPF file around the VB Script so that SCCM can execute the script from an EXE file. So in the end, I name all of my files install.vbs, install.exe, install.ipf, and install.wsm. I just have to rename them for here because I have several different installation scripts I have published.

    ReplyDelete
  5. You are the boss. One thing, how to and where to put admin user and password for user account?

    ReplyDelete
  6. You are the boss. One thing, how to and where to put admin user and password for user account?

    ReplyDelete
  7. I deployed this through SCCM thereby not needing the admin credential because it was already running as admin. It you need to run it as an admin, your best bet would probably be to use the runas.exe in front of the script with the password piped in first.

    ReplyDelete
  8. Just wanted to say thank you for this excellent script. It works a treat through SCCM 2007.

    ReplyDelete
  9. Thanks for the script!
    Is there a way to overwrite the existing font automatically? I keep getting the popup message asking me if I want to replace the font.

    ReplyDelete