• We just launched and are currently in beta. Join us as we build and grow the community.

Visual Basic .lnk (Shortcut File) Checker/Remover

sasa

Meme Lord
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
108
Likes
152
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  3. Dim

    files As

    New

    List(Of String

    )
  4. Dim

    parents As

    New

    List(Of String

    )
  5. Dim

    directories As

    New

    List(Of String

    )
  6. For

    Each

    file As

    String

    In

    My.Computer.FileSystem.GetFiles(TextBox1.Text)
  7. If

    (file.ToLower().EndsWith(".lnk"

    )) Then
  8. files.Add(file)
  9. parents.Add(TextBox1.Text)
  10. End

    If
  11. Next
  12. For

    Each

    dir As

    String

    In

    My.Computer.FileSystem.GetDirectories(TextBox1.Text)
  13. directories.Add(dir)
  14. Next
  15. Do

    Until

    directories.Count <= 0
  16. Dim

    d As

    String

    = directories(0)
  17. For

    Each

    f As

    String

    In

    My.Computer.FileSystem.GetFiles(d)
  18. If

    (f.ToLower().EndsWith(".lnk"

    )) Then
  19. files.Add(f)
  20. parents.Add(f)
  21. End

    If
  22. Next
  23. For

    Each

    di As

    String

    In

    My.Computer.FileSystem.GetDirectories(d)
  24. directories.Add(di)
  25. Next
  26. d = Nothing
  27. directories.RemoveAt(0)
  28. Loop
  29. For

    Each

    link As

    String

    In

    files
  30. With

    CreateObject

    ("Wscript.Shell"

    ).CreateShortcut(link)
  31. If

    (Not

    .targetpath.startswith("http://"

    ) And

    Not

    .targetpath.startswith("https://"

    ) And

    Not

    .targetpath.startswith("www."

    )) Then
  32. If

    (My.Computer.FileSystem.FileExists(.targetpath) Or

    My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
  33. listbox1.items.add(link & " - "

    & "Working"

    )
  34. Else
  35. listbox1.items.add(link & " - "

    & "Broken"

    )
  36. End

    If
  37. End

    If
  38. End

    With
  39. Next
  40. End

    Sub

  41. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  42. Dim

    files As

    New

    List(Of String

    )
  43. Dim

    parents As

    New

    List(Of String

    )
  44. Dim

    directories As

    New

    List(Of String

    )
  45. For

    Each

    file As

    String

    In

    My.Computer.FileSystem.GetFiles(TextBox1.Text)
  46. If

    (file.ToLower().EndsWith(".lnk"

    )) Then
  47. files.Add(file)
  48. parents.Add(TextBox1.Text)
  49. End

    If
  50. Next
  51. For

    Each

    dir As

    String

    In

    My.Computer.FileSystem.GetDirectories(TextBox1.Text)
  52. directories.Add(dir)
  53. Next
  54. Do

    Until

    directories.Count <= 0
  55. Dim

    d As

    String

    = directories(0)
  56. For

    Each

    f As

    String

    In

    My.Computer.FileSystem.GetFiles(d)
  57. If

    (f.ToLower().EndsWith(".lnk"

    )) Then
  58. files.Add(f)
  59. parents.Add(f)
  60. End

    If
  61. Next
  62. For

    Each

    di As

    String

    In

    My.Computer.FileSystem.GetDirectories(d)
  63. directories.Add(di)
  64. Next
  65. d = Nothing
  66. directories.RemoveAt(0)
  67. Loop
  68. For

    Each

    link As

    String

    In

    files
  69. With

    CreateObject

    ("Wscript.Shell"

    ).CreateShortcut(link)
  70. If

    (Not

    .targetpath.startswith("http://"

    ) And

    Not

    .targetpath.startswith("https://"

    ) And

    Not

    .targetpath.startswith("www."

    )) Then
  71. If

    (My.Computer.FileSystem.FileExists(.targetpath) Or

    My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
  72. ListBox1.Items.Add(link & " - "

    & "Working"

    )
  73. Else
  74. ListBox1.Items.Add(link & " - "

    & "Broken"

    )
  75. My.Computer.FileSystem.DeleteFile(link)
  76. ListBox1.Items.Add(link & " - "

    & "Removed"

    )
  77. End

    If
  78. End

    If
  79. End

    With
  80. Next
  81. End

    Sub
  82. End

    Class


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top