Visual Basic .lnk (Shortcut File) Checker/Remover

Introduction: Welcome to my tutorial on how to how to make a shortcut file checker and remover. Steps of Creation: Step 1: I am going to add two buttons to my program one for just checking and another for check and removing broken link files. You will also need a textbox for the path to check and a listbox to report the results. Step 2: Both the checking and the checking + removing script are very similar and it would make sense to add the same code to one function but I haven't, you can do that!
  1.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.         Dim files As New List(Of String)
  3.         Dim parents As New List(Of String)
  4.         Dim directories As New List(Of String)
  5.         For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
  6.             If (file.ToLower().EndsWith(".lnk")) Then
  7.                 files.Add(file)
  8.                 parents.Add(TextBox1.Text)
  9.             End If
  10.         Next
  11.         For Each dir As String In My.Computer.FileSystem.GetDirectories(TextBox1.Text)
  12.             directories.Add(dir)
  13.         Next
  14.         Do Until directories.Count <= 0
  15.             Dim d As String = directories(0)
  16.             For Each f As String In My.Computer.FileSystem.GetFiles(d)
  17.                 If (f.ToLower().EndsWith(".lnk")) Then
  18.                     files.Add(f)
  19.                     parents.Add(f)
  20.                 End If
  21.             Next
  22.             For Each di As String In My.Computer.FileSystem.GetDirectories(d)
  23.                 directories.Add(di)
  24.             Next
  25.             d = Nothing
  26.             directories.RemoveAt(0)
  27.         Loop
  28.         For Each link As String In files
  29.             With CreateObject("Wscript.Shell").CreateShortcut(link)
  30.                 If (Not .targetpath.startswith("http://") And Not .targetpath.startswith("https://") And Not .targetpath.startswith("www.")) Then
  31.                     If (My.Computer.FileSystem.FileExists(.targetpath) Or My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
  32.                         listbox1.items.add(link & " - " & "Working")
  33.                     Else
  34.                         listbox1.items.add(link & " - " & "Broken")
  35.                     End If
  36.                 End If
  37.             End With
  38.         Next
  39.     End Sub
So in the above script we run through each file in the given path and add it to our files list, we also get each directory and do the same for each directory so we eventually get each file from the path given in textbox1. Once we have each .lnk file we get the target path of each .lnk and make sure it is not a website link. Once we have confirmed it is a file path we check if the targetpath exists and if it does it reports working, otherwise it reports broken. That's basically the whole script, for a auto remove function we can do the same script just with an extra couple of lines...
  1.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  2.         Dim files As New List(Of String)
  3.         Dim parents As New List(Of String)
  4.         Dim directories As New List(Of String)
  5.         For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
  6.             If (file.ToLower().EndsWith(".lnk")) Then
  7.                 files.Add(file)
  8.                 parents.Add(TextBox1.Text)
  9.             End If
  10.         Next
  11.         For Each dir As String In My.Computer.FileSystem.GetDirectories(TextBox1.Text)
  12.             directories.Add(dir)
  13.         Next
  14.         Do Until directories.Count <= 0
  15.             Dim d As String = directories(0)
  16.             For Each f As String In My.Computer.FileSystem.GetFiles(d)
  17.                 If (f.ToLower().EndsWith(".lnk")) Then
  18.                     files.Add(f)
  19.                     parents.Add(f)
  20.                 End If
  21.             Next
  22.             For Each di As String In My.Computer.FileSystem.GetDirectories(d)
  23.                 directories.Add(di)
  24.             Next
  25.             d = Nothing
  26.             directories.RemoveAt(0)
  27.         Loop
  28.         For Each link As String In files
  29.             With CreateObject("Wscript.Shell").CreateShortcut(link)
  30.                 If (Not .targetpath.startswith("http://") And Not .targetpath.startswith("https://") And Not .targetpath.startswith("www.")) Then
  31.                     If (My.Computer.FileSystem.FileExists(.targetpath) Or My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
  32.                         ListBox1.Items.Add(link & " - " & "Working")
  33.                     Else
  34.                         ListBox1.Items.Add(link & " - " & "Broken")
  35.                         My.Computer.FileSystem.DeleteFile(link)
  36.                         ListBox1.Items.Add(link & " - " & "Removed")
  37.                     End If
  38.                 End If
  39.             End With
  40.         Next
  41.     End Sub
Project Complete! That's it! Below is the full source code and a download to the visual basic solution project files:
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Dim files As New List(Of String)
  5.         Dim parents As New List(Of String)
  6.         Dim directories As New List(Of String)
  7.         For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
  8.             If (file.ToLower().EndsWith(".lnk")) Then
  9.                 files.Add(file)
  10.                 parents.Add(TextBox1.Text)
  11.             End If
  12.         Next
  13.         For Each dir As String In My.Computer.FileSystem.GetDirectories(TextBox1.Text)
  14.             directories.Add(dir)
  15.         Next
  16.         Do Until directories.Count <= 0
  17.             Dim d As String = directories(0)
  18.             For Each f As String In My.Computer.FileSystem.GetFiles(d)
  19.                 If (f.ToLower().EndsWith(".lnk")) Then
  20.                     files.Add(f)
  21.                     parents.Add(f)
  22.                 End If
  23.             Next
  24.             For Each di As String In My.Computer.FileSystem.GetDirectories(d)
  25.                 directories.Add(di)
  26.             Next
  27.             d = Nothing
  28.             directories.RemoveAt(0)
  29.         Loop
  30.         For Each link As String In files
  31.             With CreateObject("Wscript.Shell").CreateShortcut(link)
  32.                 If (Not .targetpath.startswith("http://") And Not .targetpath.startswith("https://") And Not .targetpath.startswith("www.")) Then
  33.                     If (My.Computer.FileSystem.FileExists(.targetpath) Or My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
  34.                         listbox1.items.add(link & " - " & "Working")
  35.                     Else
  36.                         listbox1.items.add(link & " - " & "Broken")
  37.                     End If
  38.                 End If
  39.             End With
  40.         Next
  41.     End Sub
  42.  
  43.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  44.         Dim files As New List(Of String)
  45.         Dim parents As New List(Of String)
  46.         Dim directories As New List(Of String)
  47.         For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
  48.             If (file.ToLower().EndsWith(".lnk")) Then
  49.                 files.Add(file)
  50.                 parents.Add(TextBox1.Text)
  51.             End If
  52.         Next
  53.         For Each dir As String In My.Computer.FileSystem.GetDirectories(TextBox1.Text)
  54.             directories.Add(dir)
  55.         Next
  56.         Do Until directories.Count <= 0
  57.             Dim d As String = directories(0)
  58.             For Each f As String In My.Computer.FileSystem.GetFiles(d)
  59.                 If (f.ToLower().EndsWith(".lnk")) Then
  60.                     files.Add(f)
  61.                     parents.Add(f)
  62.                 End If
  63.             Next
  64.             For Each di As String In My.Computer.FileSystem.GetDirectories(d)
  65.                 directories.Add(di)
  66.             Next
  67.             d = Nothing
  68.             directories.RemoveAt(0)
  69.         Loop
  70.         For Each link As String In files
  71.             With CreateObject("Wscript.Shell").CreateShortcut(link)
  72.                 If (Not .targetpath.startswith("http://") And Not .targetpath.startswith("https://") And Not .targetpath.startswith("www.")) Then
  73.                     If (My.Computer.FileSystem.FileExists(.targetpath) Or My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
  74.                         ListBox1.Items.Add(link & " - " & "Working")
  75.                     Else
  76.                         ListBox1.Items.Add(link & " - " & "Broken")
  77.                         My.Computer.FileSystem.DeleteFile(link)
  78.                         ListBox1.Items.Add(link & " - " & "Removed")
  79.                     End If
  80.                 End If
  81.             End With
  82.         Next
  83.     End Sub
  84. End Class

Comments

You could clean this code up allot more instead of using the same loops over and over.

that is this ? this is a .lnk virus Remover ?

Add new comment