Thursday, March 19, 2015

AllUsersTemplates

Templates Update

Option Explicit
'------------------------------------------------------------------------------------------------
' VBScript to deploy changes & deletions of document Templates
'
' NOTE: This script copies the files to/deletes files from each users' Templates folder, e.g
'       C:\Documents and Settings\<user>\Application Data\Microsoft\Templates
'------------------------------------------------------------------------------------------------

Dim IniFile
IniFile = "BTTemplate.INI"

'------------------------------------------------------------------------
' Preparations - create objects, define variables, get values, etc.
'------------------------------------------------------------------------

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const ScriptName = "BTTemplatesUpdate.vbs"
Const INIFileName = "INIs\SiteRef.INI"
Const RemoveTemplatesFileName = "INIs\RemoveTemplates.txt"
Const AllUsersTemplatesFolder = "C:\Documents and Settings\Default User\Application Data\Microsoft\Templates"
Const UserTemplatesPath = "\Application Data\Microsoft\Templates"

' Objects
Dim WshShell
Dim ObjEnv
Dim Fso
Dim Wshnetwork

' Variables
Dim NewTemplatesFolder
Dim UserTemplatesFolder
Dim RemoveTemplates()
ReDim RemoveTemplates(0)
Dim RmvTmplCount
Dim log
Dim LogFile
Dim CurrWorkDir

' INI File values
Dim TemplatePath
Dim TemplateFileSite

Set WshShell=wscript.createobject("wscript.shell")
Set ObjEnv= wshshell.environment("Process")
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Set Wshnetwork=wscript.createobject("wscript.network")

On Error Resume Next

' Keep log in standard logs location
log = "C:\Windows\Appslogs\TemplatesUpdate.log"

'------------------------------------------------------------------------------------------------
' Ready...
'------------------------------------------------------------------------------------------------

' Set the location of the new Templates
CurrWorkDir = WshShell.CurrentDirectory & "\"


' Check for and, if necessary create the All Users Templates folder
If Not Fso.FolderExists(AllUsersTemplatesFolder) Then
Fso.CreateFolder(AllUsersTemplatesFolder)
End If

' Create or Open logfile and add entry
Set LogFile = Fso.OpenTextFile(log, ForAppending, True)
call Writelog("* * * Start Script * * *")

' Clear errors in case key doesn't exist
Err.Clear

' Check required files exist
CheckFiles

' Read SiteRef.INI and Delete Templates.txt files
ReadINIFiles
If Len(Trim(TemplatePath)) = 0 Then
' Report error and stop
ExitScript("104 No valid value for TemplatePath")

End If

If Len(Trim(TemplateFileSite)) = 0 Then
' Report error and stop
ExitScript("105 No valid value for TemplateFileSite")

End If


' Check referenced new templates folder exists
CheckTemplatesFolder

' Check correct PC for supplied templates
CheckPCTemplates

' Update the Templates held in All Users AND by each user
WriteLog("All checks passed... updating PC")
UpdateTemplates


' End
ExitScript("0 Script completed OK")

'------------------------------------------------------------------------------------------------
' Subroutines
'------------------------------------------------------------------------------------------------
' Check required files exist
Sub CheckFiles()

' Check INI and DeleteTemplates files exist...
'
' SiteRef.INI
If Not Fso.FileExists(CurrWorkDir & INIFileName) Then
  ' Report error and stop
  ExitScript("100 INI File (" & CurrWorkDir & INIFileName & ") not found")
End If

' DeleteTemplates
If Not Fso.FileExists(CurrWorkDir & RemoveTemplatesFileName) Then
  ' Report error and stop
  ExitScript("101 Delete Templates File (" & CurrWorkDir & RemoveTemplatesFileName & ") not found")
End If

End Sub
'------------------------------------------------------------------------------------------------
' Read the INI file
Sub ReadINIFiles()
Dim f1
Dim ReadLine

' Read the INI file. Store the TemplatePath and TemplateFileSite values, and build an
' array of the Templates to delete

  Set f1 = fso.OpenTextFile(CurrWorkDir & INIFileName, ForReading)
 
' Read all lines in the file
Do Until f1.AtEndOfStream
  ReadLine = Trim(f1.ReadLine)
 
  ' Ignore commented out and blank lines
  If Left(ReadLine,1) <> ";" And Len(ReadLine) > 0 Then
  ' If INI file there will be value labels - extract their values
  If Ucase(Left(ReadLine, 13)) = "TEMPLATEPATH=" Then
  TemplatePath = Mid(ReadLine,14)
WriteLog("TemplatePath = " & TemplatePath)  
 
  ElseIf Ucase(Left(ReadLine, 17)) = "TEMPLATEFILESITE=" Then
  TemplateFileSite = Mid(ReadLine,18)
WriteLog("TemplateFileSite = " & TemplateFileSite)  
 
  End If
End If
Loop

f1.Close
  Set f1 = Nothing


' Read the RemoveTemplates file and store in the RemoveTemplates() array

  Set f1 = fso.OpenTextFile(CurrWorkDir & RemoveTemplatesFileName, ForReading)
  RmvTmplCount = 0
 
' Read all lines in the file
Do Until f1.AtEndOfStream
  ReadLine = Trim(f1.ReadLine)
 
  ' Ignore commented out and blank lines
  If Left(ReadLine,1) <> ";" And Len(ReadLine) > 0 Then
 
RemoveTemplates(RmvTmplCount) = ReadLine

' Increment array count & redefine the array
RmvTmplCount = RmvTmplCount + 1
ReDim Preserve RemoveTemplates(RmvTmplCount)

End If
Loop

f1.Close
  Set f1 = Nothing

End Sub
'------------------------------------------------------------------------------------------------
' Check the Templates folder exists
Sub CheckTemplatesFolder()

If Not Fso.FolderExists(CurrWorkDir & "Templates") Then
  ' Templates folder not found - exit
  WriteLog("Error 102 Templates Folder (" & CurrWorkDir & "Templates" & "\" & TemplatePath & ") not found")
  ExitScript("102 Templates Folder not found")
  End If

End Sub
'------------------------------------------------------------------------------------------------
' Check PC has the previous version of the example Template
Sub CheckPCTemplates()

' Only need to check this is a suitable target PC if the TemplateFileSite value is not "no_check"
If Lcase(TemplateFileSite) <> "no_check" Then
' Check the named fie exists on the PC
If Not Fso.FileExists(AllUsersTemplatesFolder & "\" & TemplateFileSite) Then
' File not found - this is not a suitable PC
WriteLog("Error 103 TemplateFileSite (" & AllUsersTemplatesFolder & "\" & TemplateFileSite & ") not found")
ExitScript("103 TemplateFileSite file not found")
End If
End If

End Sub
'------------------------------------------------------------------------------------------------
' Update the Templates on the PC
Sub UpdateTemplates()
Dim f
Dim sf
Dim folderName
Dim x

' Ignore any error
On error Resume Next

NewTemplatesFolder = CurrWorkDir & "Templates" & "\" & TemplatePath

' Process every user folder (this includes Default User)
Set f = Fso.GetFolder("C:\Documents and Settings")
Set sf = f.SubFolders

' Copy to each user's Templates folder
For Each folderName In sf
WriteLog("Processing " & folderName)

' Set the Templates path
UserTemplatesFolder = folderName  & UserTemplatesPath

' If Users Templates folder does not exist, create one
If Not Fso.FolderExists(UserTemplatesFolder) Then
WriteLog(UserTemplatesFolder & " not found... creating")
Fso.CreateFolder(UserTemplatesFolder)
End If

' Copy new templates
FSO.DeleteFile(UserTemplatesFolder & "\*.*")
Fso.CopyFile NewTemplatesFolder & "\*.*", UserTemplatesFolder, True
If Err.Number <> 0 then
If Err.Number <> 76 Then ' Ignore "Path not found" - usually applies to All Users folder
call WriteLog(Err.Description & " Error # " & Err.Number & " - Possible error copying files")
End If
Err.Clear
End If

' Check for and if present delete files listed in the RemoveTemplate() array
For x = 0 to (RmvTmplCount - 1)
  If Fso.FileExists(UserTemplatesFolder & RemoveTemplates(x)) Then
  ' Delete the file
  WriteLog("Deleting file " & UserTemplatesFolder & RemoveTemplates(x))
  Fso.DeleteFile UserTemplatesFolder & RemoveTemplates(x), True
If Err.Number <> 0 then
call WriteLog(Err.Description & " Error # " & Err.Number & " - Possible error deleting file")
Err.Clear
End If
  End If
Next

' Reset user flag to force Templates location reset next time user logs on
WriteLog("Deleting user flag (" & UserTemplatesFolder & "\*Otmp.FLG" & ") to force Template folder location reset")
Fso.DeleteFile UserTemplatesFolder & "\*Otmp.FLG", True
If Err.Number <> 0 Then
' Ignore "File Not Found"
If Err.Number <> 53 Then
call WriteLog(Err.Description & " Error # " & Err.Number & " - Possible error deleting file")
End If
Err.Clear
End If

Next ' Loop to next User folder

End Sub
'------------------------------------------------------------------------------------------------
' Exit and report why
Function ExitScript(ExitText)
Dim ErrorCode

' Extract the error code from the start of the supplied text. This may not be 3 digits, so
' manipulate it
If IsNumeric(Left(ExitText,3)) Then
ErrorCode = Left(ExitText,3)
ElseIf IsNumeric("0" & Left(ExitText,2)) Then
ErrorCode = "0" & Left(ExitText,2)
Else
ErrorCode = "00" & Left(ExitText,1)
End If

' Log messages other than 999 codes
If ErrorCode <> "999" then
call Writelog("Script exiting with error: " & ExitText)
call Writelog("* * * End Script * * *")
LogFile.WriteLine
End If

' Exit the script & return the error code
Wscript.Quit ErrorCode
End Function

'------------------------------------------------------------------------------------------------
' Write entry to log
Function WriteLog(LogText)
LogFile.WriteLine Date() & " " & Time() & " " & ScriptName & ": " &  LogText
End Function

No comments:

Post a Comment