Visual Basic .lnk (Shortcut File) Checker/Remover
Submitted by Yorkiebar on Friday, September 13, 2013 - 05:02.
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!
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...
Project Complete!
That's it! Below is the full source code and a download to the visual basic solution project files:
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim files As New List(Of String)
- Dim parents As New List(Of String)
- Dim directories As New List(Of String)
- For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
- If (file.ToLower().EndsWith(".lnk")) Then
- files.Add(file)
- parents.Add(TextBox1.Text)
- End If
- Next
- For Each dir As String In My.Computer.FileSystem.GetDirectories(TextBox1.Text)
- directories.Add(dir)
- Next
- Do Until directories.Count <= 0
- Dim d As String = directories(0)
- For Each f As String In My.Computer.FileSystem.GetFiles(d)
- If (f.ToLower().EndsWith(".lnk")) Then
- files.Add(f)
- parents.Add(f)
- End If
- Next
- For Each di As String In My.Computer.FileSystem.GetDirectories(d)
- directories.Add(di)
- Next
- d = Nothing
- directories.RemoveAt(0)
- Loop
- For Each link As String In files
- With CreateObject("Wscript.Shell").CreateShortcut(link)
- If (Not .targetpath.startswith("http://") And Not .targetpath.startswith("https://") And Not .targetpath.startswith("www.")) Then
- If (My.Computer.FileSystem.FileExists(.targetpath) Or My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
- listbox1.items.add(link & " - " & "Working")
- Else
- listbox1.items.add(link & " - " & "Broken")
- End If
- End If
- End With
- Next
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- Dim files As New List(Of String)
- Dim parents As New List(Of String)
- Dim directories As New List(Of String)
- For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
- If (file.ToLower().EndsWith(".lnk")) Then
- files.Add(file)
- parents.Add(TextBox1.Text)
- End If
- Next
- For Each dir As String In My.Computer.FileSystem.GetDirectories(TextBox1.Text)
- directories.Add(dir)
- Next
- Do Until directories.Count <= 0
- Dim d As String = directories(0)
- For Each f As String In My.Computer.FileSystem.GetFiles(d)
- If (f.ToLower().EndsWith(".lnk")) Then
- files.Add(f)
- parents.Add(f)
- End If
- Next
- For Each di As String In My.Computer.FileSystem.GetDirectories(d)
- directories.Add(di)
- Next
- d = Nothing
- directories.RemoveAt(0)
- Loop
- For Each link As String In files
- With CreateObject("Wscript.Shell").CreateShortcut(link)
- If (Not .targetpath.startswith("http://") And Not .targetpath.startswith("https://") And Not .targetpath.startswith("www.")) Then
- If (My.Computer.FileSystem.FileExists(.targetpath) Or My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
- ListBox1.Items.Add(link & " - " & "Working")
- Else
- ListBox1.Items.Add(link & " - " & "Broken")
- My.Computer.FileSystem.DeleteFile(link)
- ListBox1.Items.Add(link & " - " & "Removed")
- End If
- End If
- End With
- Next
- End Sub
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim files As New List(Of String)
- Dim parents As New List(Of String)
- Dim directories As New List(Of String)
- For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
- If (file.ToLower().EndsWith(".lnk")) Then
- files.Add(file)
- parents.Add(TextBox1.Text)
- End If
- Next
- For Each dir As String In My.Computer.FileSystem.GetDirectories(TextBox1.Text)
- directories.Add(dir)
- Next
- Do Until directories.Count <= 0
- Dim d As String = directories(0)
- For Each f As String In My.Computer.FileSystem.GetFiles(d)
- If (f.ToLower().EndsWith(".lnk")) Then
- files.Add(f)
- parents.Add(f)
- End If
- Next
- For Each di As String In My.Computer.FileSystem.GetDirectories(d)
- directories.Add(di)
- Next
- d = Nothing
- directories.RemoveAt(0)
- Loop
- For Each link As String In files
- With CreateObject("Wscript.Shell").CreateShortcut(link)
- If (Not .targetpath.startswith("http://") And Not .targetpath.startswith("https://") And Not .targetpath.startswith("www.")) Then
- If (My.Computer.FileSystem.FileExists(.targetpath) Or My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
- listbox1.items.add(link & " - " & "Working")
- Else
- listbox1.items.add(link & " - " & "Broken")
- End If
- End If
- End With
- Next
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- Dim files As New List(Of String)
- Dim parents As New List(Of String)
- Dim directories As New List(Of String)
- For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
- If (file.ToLower().EndsWith(".lnk")) Then
- files.Add(file)
- parents.Add(TextBox1.Text)
- End If
- Next
- For Each dir As String In My.Computer.FileSystem.GetDirectories(TextBox1.Text)
- directories.Add(dir)
- Next
- Do Until directories.Count <= 0
- Dim d As String = directories(0)
- For Each f As String In My.Computer.FileSystem.GetFiles(d)
- If (f.ToLower().EndsWith(".lnk")) Then
- files.Add(f)
- parents.Add(f)
- End If
- Next
- For Each di As String In My.Computer.FileSystem.GetDirectories(d)
- directories.Add(di)
- Next
- d = Nothing
- directories.RemoveAt(0)
- Loop
- For Each link As String In files
- With CreateObject("Wscript.Shell").CreateShortcut(link)
- If (Not .targetpath.startswith("http://") And Not .targetpath.startswith("https://") And Not .targetpath.startswith("www.")) Then
- If (My.Computer.FileSystem.FileExists(.targetpath) Or My.Computer.FileSystem.DirectoryExists(.targetpath)) Then
- ListBox1.Items.Add(link & " - " & "Working")
- Else
- ListBox1.Items.Add(link & " - " & "Broken")
- My.Computer.FileSystem.DeleteFile(link)
- ListBox1.Items.Add(link & " - " & "Removed")
- End If
- End If
- End With
- Next
- End Sub
- End Class
Comments
Add new comment
- Add new comment
- 84 views