批量删除无效的快捷方式

时间:2014-03-17 12:21:36

标签: batch-file vbscript shortcut delete-file

如何创建一个批处理文件,用于搜索计算机中所有无效的快捷方式(不会链接到不再存在的文件的快捷方式。)并删除它们。它可以在批处理中完成,但如果在批处理中存在问题,则VBScript就可以了。

2 个答案:

答案 0 :(得分:2)

这是一个vbscript。

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

答案 1 :(得分:-1)

回馈一些东西。 KIX对我来说效果更好,我用这个脚本从用户那里删除了Citrix pnagen.exe LNK文件,因为Receiver 4.3在安装过程中没有清理:

;---------------------------------------------------------------------
; 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