' SoftGridProfileJanitor 1.0 [20070721] ' (c) 2007 Kalle Saunamäki (kalle@saunamaki.fi - http://www.virtualapp.net/), All rights reserved ' ' This script clears out leftover ICOs and OSD SCRIPTBODY -scripts in user's SoftGrid profile directory, ' optionally only reports on orphaned files but do not delete them. ' ' Run the script either with cscript.exe or wscript.exe (more useful for silent operation): ' wscript.exe //nologo SoftGridProfileJanitor.vbs Option Explicit Dim Silent, ReportOnly, SearchOrphanICOs, SearchLeftoverScripts, AgeThreshold Dim SoftGridMimePath, SoftgridMimeQueryPath, UserDataPath ReDim ICOsInUse(0) Dim WshShell, WshFSO, oExec ' Script parameters ' ------------------------------------------------ ' Change to False to get error/status messages Silent = True ' Change to False to actually delete orphaned ICOs and sw00xxx.bat scripts. ' Otherwise, report only (if Silent = False) those that are found ReportOnly = True ' Change to False to skip search of orphaned ICOs for reporting / deletion SearchOrphanICOs = True ' Change to False to skip search of orphaned sw00xx.bat files for reporting / deletion SearchLeftoverScripts = True ' How old in minutes cached script must be before it's considered for deletion, ' use for failsafe to bypass deletion of scripts that currently runs (default: 5 [minutes]) ' Requires ReportOnly = False and SearchLeftoverScripts = True to be set. AgeThreshold = 5 ' ------------------------------------------------ Function ReadSoftGridPath Dim ClientPath On Error Resume Next ClientPath = WshShell.RegRead("HKLM\SOFTWARE\Softricity\SoftGrid Client\CurrentVersion\Configuration\InstallPath") On Error Goto 0 If IsEmpty(ClientPath) Then ReadSoftGridPath = False Else ReadSoftgridPath = True SoftGridMimePath = ClientPath & "\sftmime.exe" End If End Function Function ReadUserDataPath On Error Resume Next UserDataPath = WshShell.RegRead("HKCU\Software\Softricity\SoftGrid Client\CurrentVersion\UserInfo\DataDirectory") On Error Goto 0 If IsEmpty(UserDataPath) Then ReadUserDataPath = False Else ReadUserDataPath = True End If End Function Function MatchAndPurgeIcos ' Will purge ICOs no longer associated with applications and/or FTAs Dim IcoFolderPath,IcoFile Dim oIcoFolder, oIcoFiles Dim FileToDelete ReDim ICOsToRemove(0) IcoFolderPath = UserDataPath & "\Icon Cache" If WshFSO.FolderExists(IcoFolderPath) Then Set oIcoFolder = WshFSO.GetFolder(IcoFolderPath) Set oIcoFiles = oIcoFolder.Files ' Read in ICO files in user's directory On Error Resume Next For Each IcoFile In oIcoFiles If Right(UCase(IcoFile.Path),3) = "ICO" Then If UBound(Filter(ICOsInUse,IcoFile.Path)) = -1 Then ' Icon in directory not used by App or FTA ICOsToRemove(UBound(ICOsToRemove)) = IcoFile.Path ReDim Preserve ICOsToRemove(UBound(ICOsToRemove) + 1 ) If Silent = False Then WScript.echo "Orphaned icon found: " & IcoFile.Path End If End If End If Next If ReportOnly = False Then ' Delete orphaned files For Each FileToDelete In ICOsToRemove WshFSO.DeleteFile(FileToDelete) Next End If On Error Goto 0 MatchAndPurgeIcos = True Else MatchAndPurgeIcos = False End If End Function Function PurgeScripts ' Will purge OSD scripts that might be left behind after execution Dim oScriptFolder, oScriptFiles Dim ScriptFile Dim FileAge If WshFSO.FolderExists(UserDataPath) Then Set oScriptFolder = WshFSO.GetFolder(UserDataPath) Set oScriptFiles = oScriptFolder.Files On Error Resume Next For Each ScriptFile In oScriptFiles If (Right(UCase(ScriptFile.Name),3) = "BAT") And (Left(UCase(ScriptFile.Name),2) = "SW") Then ' Check that creation date goes beyond age threshold FileAge = DateDiff("n",ScriptFile.DateCreated,Now) If FileAge > AgeThreshold Then If Silent = False Then WScript.echo "Orphaned OSD script found: " & ScriptFile.Path End If If ReportOnly = False Then WshFSO.DeleteFile(ScriptFile.Path) End If End If End If Next On Error Goto 0 PurgeScripts = True Else PurgeScripts = False End If End Function Function ParseInUseICOs Dim FileLine, LineDetails, AppFile, FtaFile Dim AppsListTempFile, FtaListTempFile Dim ServerAddrStart, ServerAddrEnd ReDim GuidList(0) Dim temp Dim CodebaseDetected, EncodingChange, BadOSD AppsListTempFile = WshShell.ExpandEnvironmentStrings("%temp%") & "\~sgprofapp.lst" FtaListTempFile = WshShell.ExpandEnvironmentStrings("%temp%") & "\~sgproffta.lst" ' Query applications SoftgridMimeQueryPath = SoftgridMimePath & " query obj:app /log " & AppsListTempFile Set oExec = WshShell.Exec(SoftgridMimeQueryPath) Do While oExec.Status = 0 WScript.Sleep 100 Loop ' Query filetypes SoftgridMimeQueryPath = SoftgridMimePath & " query obj:type /log " & FtaListTempFile Set oExec = WshShell.Exec(SoftgridMimeQueryPath) Do While oExec.Status = 0 WScript.Sleep 100 Loop If (WshFSO.FileExists(AppsListTempFile)) And (WshFSO.FileExists(FtaListTempFile)) Then Set AppFile = WshFSO.OpenTextFile(AppsListTempFile, 1) Do Until AppFile.AtEndOfStream FileLine = AppFile.Readline LineDetails = Split(FileLine , Chr(9)) ICOsInUse(UBound(ICOsInUse)) = LineDetails(3) ReDim Preserve ICOsInUse(UBound(ICOsInUse) + 1) Loop AppFile.Close() ' Delete temporary file WshFSO.DeleteFile(AppsListTempFile) Set FtaFile = WshFSO.OpenTextFile(FtaListTempFile, 1) Do Until FtaFile.AtEndOfStream FileLine = FtaFile.Readline LineDetails = Split(FileLine , Chr(9)) ICOsInUse(UBound(ICOsInUse)) = LineDetails(3) ReDim Preserve ICOsInUse(UBound(ICOsInUse) + 1) Loop FtaFile.Close() ' Delete temporary file WshFSO.DeleteFile(FtaListTempFile) ParseInUseICOs = True Else ParseInUseICOs = False End If End Function ' Main body If Silent = False Then WScript.echo "SoftGridProfileJanitor 1.0" WScript.echo "(c) 2007 Kalle Saunamäki (kalle@saunamaki.fi) - http://www.virtualapp.net/" WScript.echo "" End If ' Initialize WSH objects Set WshShell = CreateObject("WScript.Shell") Set WshFSO = CreateObject("Scripting.FileSystemObject") If (ReadSoftGridPath = True) And (ReadUserDataPath = True) Then ' Check if SoftGrid Client exists on this machine If Not (WshFSO.FileExists(SoftgridMimePath)) Then If Silent = False Then WScript.echo "Could not found SoftGrid Client from this machine!" End If Else If WshFSO.FolderExists(UserDataPath) Then ' (Optionally) purge leftover ICOs If SearchOrphanICOs = True Then If ParseInUseICOs = True Then If MatchAndPurgeIcos = False Then WScript.echo "Could not query cached ICOs!" End If Else If Silent = False Then WScript.echo "Could not query application/FTA information from client!" End If End If End If ' (Optionally) purge leftover scripts If SearchLeftoverScripts = True Then If PurgeScripts = False Then If Silent = False Then WScript.echo "Could not remove leftover scripts!" End If End If End If Else If Silent = False Then WScript.echo "SoftGrid data directory not found!" End If End If End If Else If Silent = False Then WScript.echo "SoftGrid client or user SoftGrid profile not found!" End If End If