Bah, what can I say, the idea intrigued me!
Anyway, this script will parse through the location defined in objStartFolder and look for any shortcuts pointing to locations beginning with sTargetStart and if they don't point to anything valid it deletes them. Afterwards it goes through and looks for empty folders and removes them too.
As it is set atm it looks in "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Applications" (After first checking it exists) for any shortcuts (.lnk) that point to a target starting with "C:\" and removes it if the target doesn't exist.
I've hacked this together quite quickly, so don't expect anything pretty :rolleyes2:
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
objStartFolder = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Applications"
sTargetStart = "C:\"
If objFSO.FolderExists(objStartFolder) Then
' Check for invalid shortcuts pointing to locations starting with sTargetStart
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
CheckValid objFile
Next
CheckSubFoldersShortcuts objFSO.GetFolder(objStartFolder)
' Check for empty folders after clearing out invalid shortcuts
CheckSubFoldersEmptyAndRemove objFSO.GetFolder(objStartFolder)
Else
'msgbox objStartFolder & " does not exist"
End If
Sub CheckSubFoldersEmptyAndRemove(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
If FolderEmpty(Subfolder.Path) Then
'msgbox "delete " & Subfolder.Path
objFSO.DeleteFolder Subfolder.Path
Else
'msgbox Subfolder.Path & " is occupied"
CheckSubFoldersEmptyAndRemove Subfolder
End If
Next
End Sub
Sub CheckSubFoldersShortcuts(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
CheckValid objFile
Next
CheckSubFoldersShortcuts Subfolder
Next
End Sub
Sub CheckValid(objCheckFile)
If LCase(objFSO.GetExtensionName(objCheckFile.name)) = "lnk" Then
Set oLnk = oShell.CreateShortcut(objCheckFile.path)
'msgbox objCheckFile.Path
'msgbox oLnk.TargetPath
If StrComp(LCase(Left(oLnk.TargetPath,Len(sTargetStart))),LCase(sTargetStart)) = 0 Then
If objFSO.FileExists(oLnk.TargetPath) Then
'msgbox objCheckFile.path & " is a valid shortcut"
Else
'msgbox objCheckFile.path & " is an invalid shortcut"
objFSO.DeleteFile objCheckFile.path
End If
Else
'msgbox "Not local shortcut"
End If
End If
End Sub
Function FolderEmpty(strFolderPathName)
Dim oFiles, oFile, oFolder, oSubFolders, oSubFolder
Dim blnFileFound : blnFileFound = False
Set oFolder = objFSO.GetFolder(strFolderPathName)
Set oFiles = oFolder.Files
If oFiles.Count > 1 Then
FolderEmpty = False
Exit Function
ElseIf oFiles.Count = 1 Then
For Each oFile In oFiles
If oFile.Name <> "desktop.ini" Then
FolderEmpty = False
Exit Function
End If
Next
End If
Set oSubFolders = oFolder.SubFolders
For Each oSubFolder In oSubFolders
If Not FolderEmpty(oSubFolder.Path) Then
FolderEmpty = False
Exit Function
End If
Next
FolderEmpty = True
End Function
I know my original script was batch, but I didn't think I could do this with batch, so vbs will have to do ;)