how can i search for all exe's on the c: and return it to a listbox?
Printable View
how can i search for all exe's on the c: and return it to a listbox?
Just went over a day or two this with another person in a thread.. here's a quick and dirty recursive sub i wrote that searches the folder and all subfolders for the extension you pass to it.. (well, last three letters of the filename)
Problem is, not sure of a good way to display when the search is done... because the sub is called inside of itself all the time...VB Code:
'to call the sub RecursiveList("c:\", "exe") 'the sub code.. Private Sub RecursiveList(ByVal strSearch As String, ByVal strFind As String) Try Dim MyDirectory As System.IO.Directory 'directory declaration Dim MyDirs() As String 'string array holding directory names MyDirs = MyDirectory.GetDirectories(strSearch) 'gets current directories Dim str As String For Each str In MyDirs 'loops for every string in MyDirs Dim strFiles() As String 'string array for Files in Directory strFiles = MyDirectory.GetFiles(strSearch) 'gets files in directory Dim strReturn As String For Each strReturn In strFiles 'loops for every file in the array Dim strCompare As String strCompare = Strings.Right(strReturn, 3) If strCompare = "exe" Then ListBox1.Items.Add(strCompare) Me.Refresh() End If Next 'This calls the Function inside of itself, for every directory in the initial directory, hence "Recursion" RecursiveList(str, strFind) Next Catch End Try Exit Sub
pass the listbox in and add them as you find them right in the routine
it works but i had to replace strcompare with strreturn to get the file name but now i get the full address of the file, is there anyway to just get the name?
Don't you need the full path if you're searching the entire drive? How does just a file name help you if you have no idea where it resides? If you do want to remove the path then use System.IO.Path.GetFileName. Also, I'd suggest putting all the file names in an array and then adding the array to the ListBox in a batch. Here's how I would do it:VB Code:
Private Sub AddAllExesToListBox() Dim files As String() = Me.GetFilesRecursively("C:", "*.exe") 'The following block removes the path and just leaves the file name. 'You can call GetFileNameWithoutExtension() instead if you want to remove the ".exe" as well. For i As Integer = 0 To files.GetUpperBound(0) Step 1 files(i) = IO.Path.GetFileName(files(i)) Next i Me.ListBox1.Items.AddRange(files) End Sub Private Function GetFilesRecursively(ByVal folder As String, ByVal filter As String) As String() Dim filePaths As New Specialized.StringCollection 'Add the files in the root folder. filePaths.AddRange(IO.Directory.GetFiles(folder, filter)) 'Add the files in each subfolder. For Each subfolder As String In IO.Directory.GetDirectories(folder) filePaths.AddRange(Me.GetFilesRecursively(subfolder, filter)) Next 'Create an array with the same number of elements as items in the collection. Dim files(filePaths.Count - 1) As String 'Copy the collection to the array. filePaths.CopyTo(files, 0) Return files End Function
i'm sorry for my ignorance but where do i put that code in, after the button click or what?
Here's some code that only adds the filename, replace in the "If strCompare = "exe" codeblock of what I posted before...
VB Code:
If strCompare = "exe" Then Dim I As Integer = 1 Do strCompare = Strings.Right(strReturn, I + 3) If Strings.Left(strCompare, 1) = "\" Then ListBox1.Items.Add(Strings.Right(strReturn, I + 2)) Me.Refresh() Exit Do Else I = I + 1 End If Loop End If
i did something wrong, can you look and see what i did wrong please, thank you,
VB Code:
Public Class Form3 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents ListBox1 As System.Windows.Forms.ListBox Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ListBox1 = New System.Windows.Forms.ListBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'ListBox1 ' Me.ListBox1.Location = New System.Drawing.Point(8, 88) Me.ListBox1.Name = "ListBox1" Me.ListBox1.Size = New System.Drawing.Size(456, 329) Me.ListBox1.TabIndex = 0 ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(16, 40) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 1 Me.Button1.Text = "&Go" ' 'Form3 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(472, 485) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.ListBox1) Me.Name = "Form3" Me.Text = "Form3" Me.ResumeLayout(False) End Sub #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'to call the sub RecursiveList("c:\", "exe") 'the sub code.. Private Sub RecursiveList(ByVal strSearch As String, ByVal strFind As String) Try Dim MyDirectory As System.IO.Directory 'directory declaration Dim MyDirs() As String 'string array holding directory names MyDirs = MyDirectory.GetDirectories(strSearch) 'gets current directories Dim str As String For Each str In MyDirs 'loops for every string in MyDirs Dim strFiles() As String 'string array for Files in Directory strFiles = MyDirectory.GetFiles(strSearch) 'gets files in directory Dim strReturn As String For Each strReturn In strFiles 'loops for every file in the array Dim strCompare As String strCompare = Strings.Right(strReturn, 3) If strCompare = "exe" Then ListBox1.Items.Add(strCompare) Me.Refresh() End If Next 'This calls the Function inside of itself, for every directory in the initial directory, hence "Recursion" RecursiveList(str, strFind) Next Catch End Try Exit Sub End Sub End Class
gigemboy, I don't quite understand why you continue to recommend to get all the files in a folder and then check the extension when there is another overload, which I've posted myself, that allows you to return only those files that match a filter in the first place. Also, when manipulating file and folder paths I'd strongly recommend using the members of the System.IO.Path class. The names of the members alone, e.g. GetFileName, GetExtension, GetFileNameWithoutExtension, etc., makes your code much clearer than using string manipulation functions, which don't really describe the purpose of what you're doing. Finally, it's not a good idea to add the items to the ListBox and call Me.Refresh each time because all the updating of the UI will slow down the process considerably. Unless there's a particular reason to want to see each item as it's added, it's much more efficient, and recommended in the help documentation, to add the items to an array and then call AddRange.
nevermind, i figured it out, thanks for the help guys
Well the code was just modified from a seperate thread, originally passing in a string array with filenames to search for. I understand that it would be more efficient using a filter, I just did a quick rewrite to suit his needs, I realized it wasnt the best way, thats why i labeled it "quick and dirty" hehe. Just figured to give him something to work with (something is better than nothing..). As for the listbox, the refresh was was added previously for someone else to visually see the events as it happened, because I didnt know of a way to post a message back to the user when the recursion was finished... (do you??) Same for the string manipulation.. something written quick to give him something to work with (and being that i have a lil quirk for string manipulations for some reason :) ). You post some good observations, and great advice, as always, I like the criticism, helps me learn :thumb:
It was the strcompare, huh, wrong variable :) .. hehe i did that by hand without testing it, the later code that posts the string manipulation fixes it...Quote:
Originally Posted by cdubb07