Domanda

How would I make a batch file that would search a computer for all invalid short-cuts (short-cuts that don't link to a file that isn't there any more.) and remove them. It can be done preferable in batch but VBScript is OK if there are problems doing it in batch.

È stato utile?

Soluzione

Here is a vbscript to do it.

DeleteShortCut "C:\"


Sub DeleteShortcut(Folder)
    For Each Subfolder in Folder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files 
        For Each objFile in colFiles
            If  objFile.Name <> "Desktop.lnk"  And  UCase(objFile.Type) = "SHORTCUT" Then 
                Set objshortcut = objshell.CreateShortcut(objFile.Path)
                filepath =  objshortcut.TargetPath 
                If objFSO.FileExists(filepath) = False Then 
                    WScript.Echo  "Removing invalid ShortCut :" & objFile.Path
                    objFSO.DeleteFile(objFile.Path) 
                End If 
            End If    
        Next
        DeleteShortcut Subfolder
    Next
End Sub

Altri suggerimenti

To give something back. KIX works better for me and I made this script for removing Citrix pnagen.exe LNK files from a user as Receiver 4.3 does not clean up during install:

;---------------------------------------------------------------------
; Filename..: RemoveIcaLNK.kix
; Author....: Arild Skullerud inspired by enumfiles by David M. Stein
; Date......: 09/24/15
; Purpose...: Remove all LNK files with a set of folders
;             Removes only LNK files pointing to:
;               "C:\Program Files (x86)\Citrix\ICA Client\pnagent.exe"
;             Created with the purpose of cleaning up LNK files before
;             installing Citrix Receiver 4.3 as it fails cleaning up.
;---------------------------------------------------------------------

; Simple folders I could remove in a simple way
RD ("%AppData%\Microsoft\Windows\Start Menu\Programs\XenApp\") /s
RD ("%AppData%\Microsoft\Windows\Start Menu\Programs\Metaframe\") /s

;--------------------------------------------------------------------------------------------------------------
; Deletes $lnkfilepath pointing to the lnk file to be deleted
; $targetpath is the EXE the lnk points to ( or any other filetype)
;--------------------------------------------------------------------------------------------------------------
Function WshRmInvShortCut($lnkfilepath,$targetpath)
dim $shell,$shortcut
$shell = createobject("wscript.shell")
if $shell
  $shortcut = $shell.createshortcut("$lnkfilepath")
  if $shortcut
   if $shortcut.targetpath=$targetpath 
   ; Some debug lines
   ;? $shortcut.targetpath
   ;? $lnkfilepath
    ; Delete only the LNK file that points to the correct exe here
    ;DEL ($lnkfilepath) /h /s
    $shortcut = 0
   endif
  endif
  $shell = 0
endif
exit @error
EndFunction

;--------------------------------------------------------------------------------------------------------------
; Searches $StartPath for files ending with $ext and calls WshRmInvShortCut for removing them
; $targetpath is the exe the lnk points to
;--------------------------------------------------------------------------------------------------------------

Function Enum_Files($StartPath, $Ext)
    Dim $fName
    $fName = Dir("$StartPath\*.$Ext")
    While $fName <> "" And @ERROR = 0
        WshRmInvShortCut($StartPath+\+$fname,$targetpath)
        $fName = Dir()
    Loop
EndFunction

;--------------------------------------------------------------------------------------------------------------
; Script running from here
; Path to pnagent.exe
$targetpath = "C:\Program Files (x86)\Citrix\ICA Client\pnagent.exe"
; Filename ending
$Ext = "lnk"

; Path to folder I want to search and empty of lnk files
$StartPath="%appdata%\Microsoft\Windows\Start Menu\Programs"
; Calling script for first path
$=Enum_Files($StartPath, $Ext)

; Path to folder I want to search and empty of lnk files
$StartPath="%userprofile%\Desktop"
; Calling script for second path
$=Enum_Files($StartPath, $Ext)

:END
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top