GoGreen PC Tune-Up™
Learn More

Insta-Install™
this is how ssl encrypt our websites
MTNCOMP | List View | Table View | myBlog (1777 Entries)
myBlog Home

Blog


Funky File System Fixer - Search and View funky files by extension on Windows or external hard drives [vb.net]

Microsoft Windows

by Mountain Computers Inc., Publication Date: Tuesday, November 3, 2020

View Count: 1083, Keywords: Funky File System Fixer, Download, Look for wacky files by extension, VB.Net, Hashtags: #FunkyFileSystemFixer #Download #Lookforwackyfilesbyextension #VB.Net



This is some VB.Net sample code for a tool I wanted to crank out to scan for crazy files in your system that probably don't belong there. Normally, just finding the files is the trick and manually removing them is the second trick, and then running ccleaner to pickup the difference and finish the job.
 
here is the code, my attempt at open source yet then again:
 
https://mountaincomputers.org/downloads/ffsf.zip  (the exe is within the zip file, not code signed.)
 
Imports System.IO
Imports System.Security.Principal

Public Class Form1
    Public drive_array(100)
    Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
        Application.Exit()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call mySetup()
    End Sub
    Private Sub mySetup()

        ListBox1.Items.Clear()
        CbDrives.Items.Clear()
        ChkBxMyProfileOnly.Checked = False
        'for future usage
        BtnNotepad.Visible = False

        If F_UserIsAdministrator() Then
            ListBox1.Items.Add("User is admin. entire drive structure is available to scan.")
        Else
            ListBox1.Items.Add("User is not admin. only user profile is available to scan.")
            ListBox1.Items.Add("If you want to run in admin mode, exit, and run as administrator.")
        End If

        'check to see if there are local command scripts to verify existence if needed.
        Call CheckLocalCMDScripts()

        Try
            Debug.Print("USERPROFILE = " & My.Application.GetEnvironmentVariable("USERPROFILE"))
        Catch ex As System.ArgumentException
            Debug.Print("Environment variable 'USERPROFILE' does not exist.")
        End Try
        Call LoadCbSSPValues()
        Call GetDrives()
        TxtSSP.Text = "{*.*"
        CbDrives.Text = "select your target drive"
        Label1.Text = "Ready."
        ListBox1.Items.Add("1. select your search string pattern, or type a custom string")
        ListBox1.Items.Add("2. select your target drive.")
        ListBox1.Items.Add("3. click Scan System.")
        ListBox1.Items.Add("*. result. scan will start. click clear/reset to start over.")
        ListBox1.Items.Add("")
        ListBox1.Items.Add("NO WARRANTIES. USE AT YOUR OWN RISK.")
    End Sub
    Private Sub CheckLocalCMDScripts()

        'check to see if the main cmd script exists, otherwise we might have to generate it.
        If My.Computer.FileSystem.FileExists("scan_drive.cmd") Then
            Debug.Print("File found.")
        Else
            Debug.Print("File Not found.")
        End If

    End Sub

    Private Sub LoadCbSSPValues()
        CbSSP.Items.Add("{*.*")
        CbSSP.Items.Add("*.txt")
        CbSSP.Items.Add("*.html")
        CbSSP.Items.Add("*.c")
        CbSSP.Items.Add("*.h")
        CbSSP.Items.Add("*.ccp")
        CbSSP.Items.Add("*.log")
        CbSSP.Items.Add("*.sln")
        CbSSP.Items.Add("*.bmp")
        CbSSP.Items.Add("*.jpg")
        CbSSP.Items.Add("*.png")
        CbSSP.Items.Add("*.tif")
        CbSSP.Items.Add("*.pdf")
        CbSSP.Items.Add("*.*")
        CbSSP.Text = "select a search pattern."
    End Sub
    Private Sub BtnScanSystem_Click(sender As Object, e As EventArgs) Handles BtnScanSystem.Click

        If CbDrives.SelectedIndex >= 0 Or CbDrives.Text = "not applicable" Then

            If ChkBxMyProfileOnly.Checked Then
                'my profile only
                Debug.Print("my profile only")
                ListBox1.Items.Add("My Profile only being searched: " & My.Application.GetEnvironmentVariable("USERPROFILE"))
                ListBox1.Items.Add("This won't take too long. Stand by.")
            Else
                ListBox1.Items.Add("This might take a while. Stand by.")
            End If

            Call RecursiveSearch(CbDrives.Text, TxtSSP.Text)

        Else
            ListBox1.Items.Add("** WARNING ** Select a target drive to scan.")
        End If

    End Sub
    Private Sub GetDrives()
        'get the hard drives on the system
        Dim my_counter As Integer
        Dim my_drives As String

        my_drives = "Drives Found: "
        my_counter = 0
        Dim drives As String() = System.IO.Directory.GetLogicalDrives()
        For Each drive As String In drives
            System.Console.WriteLine(drive)
            Console.ReadLine()
            'ListBox1.Items.Add(Console.WriteLine)
            drive_array(my_counter) = drive
            CbDrives.Items.Add(drive)
            my_counter += 1
        Next
        Dim x As Integer
        For x = 0 To my_counter - 1
            my_drives = my_drives & drive_array(x) & " "
        Next

        'ListBox1.Items.Add(my_drives)
        Debug.Print(my_drives)

    End Sub

    Private Sub RecursiveSearch(cbd, ssp)
        Dim searcher As New Task(Of IList(Of String))(Function()
                                                          If ChkBxMyProfileOnly.Checked Then
                                                              Return GetFiles(My.Application.GetEnvironmentVariable("USERPROFILE"), TxtSSP.Text)
                                                          Else
                                                              Return GetFiles(cbd, ssp)
                                                          End If
                                                      End Function)
        searcher.Start()
        searcher.Wait()
        Dim files = searcher.Result
        ListBox1.Items.Add(String.Format("{0:N0} files found of pattern ", files.Count) & TxtSSP.Text & " on " & CbDrives.Text)

        For Each f As String In files
            ListBox1.Items.Add(f)
            Debug.Print(f)
        Next

    End Sub
    Function GetFiles(directoryPath As String, filter As String) As IList(Of String)
        Dim files As New List(Of String)
        Dim directories() As String = Nothing
        Try
            files.AddRange(Directory.GetFiles(directoryPath, filter))
            directories = Directory.GetDirectories(directoryPath)
        Catch ex As Exception
            'Console.WriteLine($"Error while processing folder {directoryPath}.")
            Debug.Print($"Error while processing folder {directoryPath}.")
        End Try
        If directories IsNot Nothing Then
            For Each directory In directories
                files.AddRange(GetFiles(directory, filter))
            Next
        End If
        Return files
    End Function
    Private Sub BtnHelp_Click(sender As Object, e As EventArgs) Handles BtnHelp.Click

        Dim m As String
        m = "HELP INFORMATION" & vbCrLf & vbCrLf
        m = m & "The following is what we are searching for and viewing, aka looking for:" & vbCrLf & vbCrLf
        m = m & "FILES that contain the following:" & vbCrLf
        m = m & "1. files and folders the lead with a { brace in inappropriate directories." & vbCrLf
        m = m & "2. strange files in the wrong places." & vbCrLf
        m = m & "3. leftover files that should not be hanging around" & vbCrLf & vbCrLf
        m = m & "Search String Pattern:" & vbCrLf
        m = m & "* type in the search pattern you are looking for. the default is {*.*" & vbCrLf & vbCrLf
        m = m & "CREDITS:" & vbCrLf & vbCrLf
        m = m & "Icon Attribution" & vbCrLf
        m = m & "Sprout free vector icons designed by Freepik" & vbCrLf
        m = m & "https://www.flaticon.com/free-icon/ via @flaticon " & vbCrLf & vbCrLf
        m = m & "NO WARRANTIES. USE AT YOUR OWN RISK." & vbCrLf & vbCrLf
        m = m & "Alpha version 0.5.0 Source code @ https://bit.ly/34UakCw" & vbCrLf & vbCrLf
        m = m & "(c) 2020 Mountain Computers Inc. All rights reserved." & vbCrLf & vbCrLf
        MsgBox(m, vbInformation, "Funky File System Fixer - Search and View - version 0.5.0")
    End Sub

    Private Function F_UserIsAdministrator()
        Dim W_Id = WindowsIdentity.GetCurrent()
        Dim WP = New WindowsPrincipal(W_Id)
        Dim isAdmin As Boolean = WP.IsInRole(WindowsBuiltInRole.Administrator)
        F_UserIsAdministrator = isAdmin
    End Function

    Private Sub CbSSP_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CbSSP.SelectedIndexChanged
        TxtSSP.Text = CbSSP.Text
    End Sub

    Private Sub BtnClearReset_Click(sender As Object, e As EventArgs) Handles BtnClearReset.Click
        Call mySetup()
    End Sub

    Private Sub ChkBxMyProfileOnly_CheckedChanged(sender As Object, e As EventArgs) Handles ChkBxMyProfileOnly.CheckedChanged
        If ChkBxMyProfileOnly.Checked Then
            CbDrives.Text = "not applicable"
        Else
            CbDrives.Text = "select a target drive"
        End If
    End Sub

    Private Sub CbDrives_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CbDrives.SelectedIndexChanged
        Debug.Print(CbDrives.SelectedItem & " (" & CbDrives.SelectedIndex & ")")
    End Sub
End Class
 

more to come...

if you found this article helpful, consider contributing $10, 20 an Andrew Jackson or so..to the author. more authors coming soon
FYI we use paypal or patreon, patreon has 3x the transaction fees, so we don't, not yet.

© 2024 myBlog™ v1.1 All rights reserved. We count views as reads, so let's not over think it.