The purpose of this code is to run a powerpoint presentation in a kiosk, but check every half hour to see if there are updates. If there are, the script kills the current presentation, backs up the current presentation, then copies the new presentation over the current one and re-starts the viewer. My apologies if the format makes people twitch; I came from a C-based programming environment before someone asked me to write my very first VBScript.
Your comments and feedback to streamline the code and make me a better scripter would be greatly appreciated.
EDIT: Wow, the code viewer really doesn't like my formatting. If it's unreadable, I'll just delete it.
' ------------------------------ Launch_PPT_ThinClient.cmd ---------------------------
'
'
' Batch Command to launch a powerpoint presentation, then sleep and restart the
' presentation periodically.
'
' Original Author: Bryan X
' Rewritten by: Michael X
'
' Originally Created: 5/23/2011
' Rewritten on: 9/21/2011
'
' Usage: This batch command is started by including a shortcut in the user
' profile's startup folder
'
' History:
' Version 1.0 - Original Release
' Version 2.0 - Test Release
' Version 2.1 - Production Release
'
' Notes: The powerpoint file must reside in a specific location that the service
' account has read access to. In order for the viewer to loop the presentation,
' the Slide Show settings must conform to Show type: Presented by a speaker and
' Show options Loop continuously until 'Esc'. No other settings appear to work
' in test.
'
' ----------------------------- Launch_PPT_ThinClient.cmd ------------------------------ Option Explicit 'Enforce strict coding Dim CFO, LFO, NFO
Dim CF, LF, NF
Dim NewPresentation
Dim currentfile, logfile, oldfile, newfile, PPT
Dim WhoAmI Function Launch_Presentation()
Dim WshShell : Set WshShell = WScript.CreateObject("WScript.Shell") NewPresentation = Null
WhoAmI = WshShell.ExpandEnvironmentStrings("%USERNAME%")
currentfile = "C:\Documents and Settings\" & WhoAmI & _
"\Desktop\SITECOMMUNICATION.PPT"
oldfile = "C:\Documents and Settings\" & WhoAmI & _
"\Desktop\SITECOMMUNICATION_OLD.PPT"
logfile = "C:\Documents and Settings\" & WhoAmI & _
"\Desktop\Update_Log.txt"
newfile = "G:\communication\Newtvupdate\Newplasma.ppt"
PPT= """C:\Program Files\Microsoft Office\Office14\PPTVIEW.EXE""" Check_Presentation Run_Presentation
End Function Sub Replace_Presentation()
Set CFO = CreateObject("Scripting.FileSystemObject")
Set LFO = CreateObject("Scripting.FileSystemObject")
Set NFO = CreateObject("Scripting.FileSystemObject")
Set LF = LFO.OpenTextFile(logfile, 8, True) 'Keep a log of when updates occur.
Set NF = NFO.GetFile(newfile)
NewPresentation = "Replaced" Kill_Presentation 'Turn off the PPTVIEWer so we can relaunch with the new file. If CFO.FileExists(currentfile) Then
Set CF = CFO.GetFile(currentfile)
CF.Copy oldfile, True 'Back up presentation
End If
NF.Copy currentfile, True 'Replace presentation
LF.WriteLine("Presentation file updated: " & Now()) Set CF = Nothing : Set CFO = Nothing : Set LF = Nothing : Set LFO = Nothing
Set NF = Nothing : Set NFO = Nothing
End Sub Sub Kill_Presentation() 'Find the Powerpoint Viewer under local username and kill it.
Dim objProcess
With GetObject("winmgmts:root\cimv2")
For Each objProcess in .ExecQuery ("SELECT commandline FROM Win32_Process" _
& " Where Name = 'PPTVIEW.EXE'",,48)
With .ExecMethod ("Win32_Process.Handle=" & objProcess.Handle & "", "GetOwner")
If Lcase(.User) = WhoAmI Then
objProcess.Terminate()
End If
End With
Next
End With
Set objProcess = Nothing
End Sub Sub Script_Sleep()
WScript.Sleep 1800000 'Sleep for 30 minutes.
Check_Presentation
If IsNull(NewPresentation) = True Then
Kill_Presentation
WScript.Sleep 500
Run_Presentation
Else
Script_Sleep
End If
End Sub Sub Run_Presentation()
Dim WshShell : Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run PPT & " """ & currentfile & """"
WScript.Sleep 50
WshShell.AppActivate "SITECOMMUNICATION.PPT - Microsoft PowerPoint Viewer"
WScript.Sleep 100
WshShell.SendKeys "{F5}" 'We have to send this key command in order to enable looping.
NewPresentation = Null
Set WshShell = Nothing
Script_Sleep
End Sub Sub Check_Presentation()
Set NFO = CreateObject("Scripting.FileSystemObject")
Set CFO = CreateObject("Scripting.FileSystemObject") If Not NFO.FileExists(newfile) Then
WScript.echo "Create a new presentation named " & newfile & "."
WScript.quit
End If If Not CFO.FileExists(currentfile) Then
Replace_Presentation
End If Set NF = NFO.GetFile(newfile)
Set CF = CFO.GetFile(currentfile) If NF.DateLastModified > CF.DateLastModified Then
Replace_Presentation
End If
Set CF = Nothing : Set CFO = Nothing : Set NF = Nothing : Set NFO = Nothing
End Sub Launch_Presentation
<message edited by Vick on Tuesday, October 04, 2011 5:22 AM>