Tuesday, October 18, 2011

Folder Polling Script

A neat little script which checks a specified folder (here its C:\Presentations) for the addition of a new file every 5 seconds (WITHIN 5 WHERE) .
Then the script checks for a certain file type, here Powerpoint or web page/link, and then triggers the app accordingly to run this file.
When a new file is detected, the app currently running is closed and a new one started.

I have a server connected to a plasma display, and this script allows users to drag a file into a shared folder, and the server will display the latest file dragged in to the plasma.

(It uses the relevant switches for powerpoint presentation mode - POWERPNT.EXE /S, and  Internet Explorer kiosk mode - iexplore.exe -k.)

Set objShell = CreateObject("Wscript.Shell")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\cimv2")
    'set poll time and location in the next line
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\Presentations""'")
    Set objLatestEvent = colMonitoredEvents.NextEvent 'waits until next event - next file put into folder.
    strNewFile = objLatestEvent.TargetInstance.PartComponent
    arrNewFile = Split(strNewFile, "=")
    strFileName = arrNewFile(1)
    strFileName = Replace(strFileName, "\\", "\")
    strFileName = Replace(strFileName, Chr(34), "")
    strFileName = Chr(34) & strFileName & Chr(34)
    'check for file type and if ppt or htm then close program to allow for new application instance with file
    'filter for ppt or iexplore
    If Right(strfilename,6) = ".pptx" & Chr(34) Or Right(strfilename,5) = ".ppt"  & Chr(34) Then
    objShell.Run("POWERPNT.EXE /S " & strFileName)
    ElseIf Right(strfilename,6) = ".html"  & Chr(34) or Right(strfilename,5) = ".htm"  & Chr(34) or Right(strfilename,5) = ".url"  & Chr(34) Then
    objShell.Run("iexplore.exe -k " & strFileName)
    End If
Loop 'keep going

Sub chkPPT
set service = GetObject ("winmgmts:")
bFlag = False
For each Process in Service.InstancesOf ("Win32_Process")
If Process.Name = "POWERPNT.EXE" Or process.name = "iexplore.exe" Then
bFlag = True
End If
'check for file type of newly added file - do nothing if not ppt or ie8
If Not (Right(strFileName,6) = ".pptx" & Chr(34) Or Right(strFileName,5) = ".ppt" _
& Chr(34) Or Right(strFileName,6) = ".html"  & Chr(34) OR Right(strFileName,5) = ".htm"  & Chr(34) OR Right(strFileName,5) = ".url"  & Chr(34)) Then
bFlag = False
End If
'quit applications
If bFlag Then
strWmiq = "select * from Win32_Process where name='POWERPNT.exe'"
Set objQResult = Service.Execquery(strWmiq)
For Each objProcess In objQResult
intRet = objProcess.Terminate(1)
strWmiq = "select * from Win32_Process where name='iexplore.exe'"
Set objQResult = Service.Execquery(strWmiq)
For Each objProcess In objQResult
intRet = objProcess.Terminate(1)
End If
End Sub