'Open remote backup database and scan backupset for obsolete entries. Prompt user if they want to remove.

'Get catalog location from Registry
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Quantum Tech, Inc.\Remote Backup\Settings"
strEntryName = "ClientDB"
objReg.GetExpandedStringValue HKEY_LOCAL_MACHINE, strKeyPath, strEntryName, strValue
Database = strValue & "\" & "backup.mdb"
'Wscript.Echo "Version = " & Database

status = MsgBox("Scan backup definition for file: " & Database & " ?" & vbCRLF, vbOkCancel + vbInformation + vbDefaultButton2, "Scan backupset")
If status = vbCancel Then
  Wscript.Quit
End If

'Connect to the database
Set conn = CreateObject("ADODB.Connection")
Set backupset = CreateObject("ADODB.Recordset")
strConnect = "Provider=MSDASQL; DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & DataBase & ";UID=admin;PWD=;"
conn.Open strConnect

'Query to get the backup set definition
sqlStmt = "Select * FROM Backupset ORDER by FilePath"
backupset.open sqlStmt, conn, 3, 3

'loop through file/folder specifications and prompt user to delete if they do not exist
Set objFSO = CreateObject("Scripting.FileSystemObject")
Removed = 0
Do Until backupset.EOF
  If backupset("FilePath") = "USER_TYPE" Then
    'Wscript.Echo "USER_TYPE Found."
  ElseIf backupset("FilePath") = "ADDED_PLUGINS" Then
    'Wscript.Echo "ADDED_PLUGINS Found."
  ElseIf backupset("FilePath") = "DISKDRIVE" Then
    If objFSO.DriveExists(Left(backupset("FileName"),Len(backupset("FileName"))-1)) = FALSE Then
    'Disk drive doesn't exist or not mapped - prompt to remove
      Decision = MsgBox("Drive: " & backupset("FileName") & " not found. " & VbCrLF & _
             "Remove from backup set definition?" , vbYesNo + vbInformation + vbDefaultButton2, "Missing Drive")
      If Decision = vbYes Then
        backupset.delete
        removed = removed + 1
      End If
    End If      
    'Wscript.Echo "DISKDRIVE Found."
  ElseIf backupset("FilePath") = "HKEY_CLASSES_ROOT" Then
    'Wscript.Echo "HKEY_CLASSES_ROOT Found."
  ElseIf Len(backupset("FileExt") & " ") = 1 Then
    If objFSO.FolderExists(backupset("FilePath") & "\" & backupset("FileName")) = FALSE Then
      'Folder doesn't exist. Prompt user to remove.
      Decision = MsgBox("Folder: " & backupset("FilePath") & "\" & backupset("FileName") & " not found. " & VbCrLF & _
             "Remove from backup set definition?" , vbYesNo + vbInformation + vbDefaultButton2, "Missing Folder")
      If Decision = vbYes Then
        backupset.delete
        removed = removed + 1
      End If      
    End If
    'Wscript.Echo "Folder found: " & backupset("FilePath") & "\" & backupset("FileName")
  ElseIf Len(backupset("FileExt") & " ") > 1 Then
    If objFSO.FileExists(backupset("FilePath") & "\" & backupset("FileName")) = FALSE Then
      'File doesn't exist. Prompt user to remove.
      Decision = MsgBox("File: " & backupset("FilePath") & "\" & backupset("FileName") & " not found. " & VbCrLF & _
             "Remove from backup set definition?" , vbYesNo + vbInformation + vbDefaultButton2, "Missing File")
      If Decision = vbYes Then
        backupset.delete
        removed = removed + 1
      End If      
    End If
    'Wscript.Echo "File found: " & backupset("FilePath") & "\" & backupset("FileName")
  Else
    Wscript.Echo "Unrecognized Catalog Entry - skipped ID: " & backupset("ID")
  End If
  backupset.MoveNext
Loop
If removed > 0 then
  status = MsgBox("Backupset Scan Complete. " & removed & " entries deleted from backup set definition.", vbOKONLY, "Scan Complete")
Else
  status = MsgBox("Backupset Scan Complete. No missing drives, folders or files located." , vbOKONLY, "Scan Complete")
End If
WScript.Quit
