Results 1 to 12 of 12

Thread: [resolved]search for exe's

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    307

    [resolved]search for exe's

    how can i search for all exe's on the c: and return it to a listbox?
    Last edited by cdubb07; Oct 20th, 2005 at 12:10 AM.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: search for exe's

    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)
    VB Code:
    1. 'to call the sub
    2. RecursiveList("c:\", "exe")
    3.  
    4. 'the sub code..
    5. Private Sub RecursiveList(ByVal strSearch As String, ByVal strFind As String)
    6.         Try
    7.             Dim MyDirectory As System.IO.Directory 'directory declaration
    8.             Dim MyDirs() As String 'string array holding directory names
    9.             MyDirs = MyDirectory.GetDirectories(strSearch) 'gets current directories
    10.             Dim str As String
    11.             For Each str In MyDirs 'loops for every string in MyDirs
    12.                 Dim strFiles() As String 'string array for Files in Directory
    13.                 strFiles = MyDirectory.GetFiles(strSearch) 'gets files in directory
    14.                 Dim strReturn As String
    15.                 For Each strReturn In strFiles 'loops for every file in the array
    16.                     Dim strCompare As String
    17.                     strCompare = Strings.Right(strReturn, 3)
    18.                     If strCompare = "exe" Then
    19.                         ListBox1.Items.Add(strCompare)
    20.                         Me.Refresh()
    21.                     End If
    22.                 Next
    23.                 'This calls the Function inside of itself, for every directory in the initial directory, hence "Recursion"
    24.                 RecursiveList(str, strFind)
    25.             Next
    26.  
    27.         Catch
    28.         End Try
    29. Exit Sub
    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...
    Last edited by gigemboy; Oct 19th, 2005 at 08:39 PM.

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: search for exe's

    pass the listbox in and add them as you find them right in the routine

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    307

    Re: search for exe's

    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?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: search for exe's

    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:
    1. Private Sub AddAllExesToListBox()
    2.         Dim files As String() = Me.GetFilesRecursively("C:", "*.exe")
    3.  
    4.         'The following block removes the path and just leaves the file name.
    5.         'You can call GetFileNameWithoutExtension() instead if you want to remove the ".exe" as well.
    6.         For i As Integer = 0 To files.GetUpperBound(0) Step 1
    7.             files(i) = IO.Path.GetFileName(files(i))
    8.         Next i
    9.  
    10.         Me.ListBox1.Items.AddRange(files)
    11.     End Sub
    12.  
    13.     Private Function GetFilesRecursively(ByVal folder As String, ByVal filter As String) As String()
    14.         Dim filePaths As New Specialized.StringCollection
    15.  
    16.         'Add the files in the root folder.
    17.         filePaths.AddRange(IO.Directory.GetFiles(folder, filter))
    18.  
    19.         'Add the files in each subfolder.
    20.         For Each subfolder As String In IO.Directory.GetDirectories(folder)
    21.             filePaths.AddRange(Me.GetFilesRecursively(subfolder, filter))
    22.         Next
    23.  
    24.         'Create an array with the same number of elements as items in the collection.
    25.         Dim files(filePaths.Count - 1) As String
    26.  
    27.         'Copy the collection to the array.
    28.         filePaths.CopyTo(files, 0)
    29.  
    30.         Return files
    31.     End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    307

    Re: search for exe's

    i'm sorry for my ignorance but where do i put that code in, after the button click or what?

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: search for exe's

    Here's some code that only adds the filename, replace in the "If strCompare = "exe" codeblock of what I posted before...
    VB Code:
    1. If strCompare = "exe" Then
    2.                         Dim I As Integer = 1
    3.                         Do
    4.                             strCompare = Strings.Right(strReturn, I + 3)
    5.                             If Strings.Left(strCompare, 1) = "\" Then
    6.                                 ListBox1.Items.Add(Strings.Right(strReturn, I + 2))
    7.                                 Me.Refresh()
    8.                                 Exit Do
    9.                             Else
    10.                                 I = I + 1
    11.                             End If
    12.                         Loop
    13.                     End If

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    307

    Re: search for exe's

    i did something wrong, can you look and see what i did wrong please, thank you,

    VB Code:
    1. Public Class Form3
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'Form overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.     Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    33.     Friend WithEvents Button1 As System.Windows.Forms.Button
    34.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    35.         Me.ListBox1 = New System.Windows.Forms.ListBox
    36.         Me.Button1 = New System.Windows.Forms.Button
    37.         Me.SuspendLayout()
    38.         '
    39.         'ListBox1
    40.         '
    41.         Me.ListBox1.Location = New System.Drawing.Point(8, 88)
    42.         Me.ListBox1.Name = "ListBox1"
    43.         Me.ListBox1.Size = New System.Drawing.Size(456, 329)
    44.         Me.ListBox1.TabIndex = 0
    45.         '
    46.         'Button1
    47.         '
    48.         Me.Button1.Location = New System.Drawing.Point(16, 40)
    49.         Me.Button1.Name = "Button1"
    50.         Me.Button1.TabIndex = 1
    51.         Me.Button1.Text = "&Go"
    52.         '
    53.         'Form3
    54.         '
    55.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    56.         Me.ClientSize = New System.Drawing.Size(472, 485)
    57.         Me.Controls.Add(Me.Button1)
    58.         Me.Controls.Add(Me.ListBox1)
    59.         Me.Name = "Form3"
    60.         Me.Text = "Form3"
    61.         Me.ResumeLayout(False)
    62.  
    63.     End Sub
    64.  
    65. #End Region
    66.  
    67.  
    68.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    69.         'to call the sub
    70.         RecursiveList("c:\", "exe")
    71.  
    72.         'the sub code..
    73. Private Sub RecursiveList(ByVal strSearch As String, ByVal strFind As String)
    74.         Try
    75.             Dim MyDirectory As System.IO.Directory 'directory declaration
    76.             Dim MyDirs() As String 'string array holding directory names
    77.             MyDirs = MyDirectory.GetDirectories(strSearch) 'gets current directories
    78.             Dim str As String
    79.             For Each str In MyDirs 'loops for every string in MyDirs
    80.                 Dim strFiles() As String 'string array for Files in Directory
    81.                 strFiles = MyDirectory.GetFiles(strSearch) 'gets files in directory
    82.                 Dim strReturn As String
    83.                 For Each strReturn In strFiles 'loops for every file in the array
    84.                     Dim strCompare As String
    85.                     strCompare = Strings.Right(strReturn, 3)
    86.                     If strCompare = "exe" Then
    87.                         ListBox1.Items.Add(strCompare)
    88.                         Me.Refresh()
    89.                     End If
    90.                 Next
    91.                 'This calls the Function inside of itself, for every directory in the initial directory, hence "Recursion"
    92.                 RecursiveList(str, strFind)
    93.             Next
    94.  
    95.         Catch
    96.         End Try
    97.         Exit Sub
    98.  
    99.  
    100.     End Sub
    101. End Class

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: search for exe's

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    307

    Re: search for exe's

    nevermind, i figured it out, thanks for the help guys

  11. #11
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [resolved]search for exe's

    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

  12. #12
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: search for exe's

    Quote Originally Posted by cdubb07
    nevermind, i figured it out, thanks for the help guys
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width