Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:01:20+00:00 2026-06-13T02:01:20+00:00

I have a listbox with directories (not local, it is over a network) listed

  • 0

I have a listbox with directories (not local, it is over a network) listed in it.
I am trying to loop through that listbox and remove any empty directories.
However, I noticed that it just skips some empty directories.. I found that if I ran the loop 3 – 5 times it will get them all, but that isn’t very efficient.

My Loop:

Dim i As Integer
i = 0
While i < ListBox1.Items.Count
   If IO.Directory.GetFiles(ListBox1.Items.Item(i), "*.*").Length = 0 Then
      ListBox1.Items.RemoveAt(i)
   End If
i = i + 1
End While

So I was just wondering if there is a more efficient way to check the contents of the directory or another way i could achieve this without having to run the loop multiple times.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:01:21+00:00Added an answer on June 13, 2026 at 2:01 am

    ListBox1.Items.Count is getting reevaluated every time, making your loop get shorter as time goes on missing some items. A quick solution could be just mark which items you want to remove as you loop.

    Dim toRemove As New List(Of ListBoxItem)
    For Each item as ListBoxItem in ListBox1.Items
       If IO.Directory.GetFiles(item, "*.*").Length = 0 Then
          toRemove.Add(item)
       End If
    Next
    
    For Each item as ListBoxItem in toRemove
        ListBox1.Items.Remove(item)
    Next
    

    (there is probably a better way, but that’s a quick solution off the top of my head)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listbox of checkboxes that I want to iterate through and check
I have a ListBox with items that fill TextBox es. How do I identify
I have a listbox for a Windows Phone 7 app that displays info parsed
I have a ListBox defined in my app that I've populated with some static
I have a listbox that I am populating using a foreach. (Long explanation of
I have a Listbox that is binded to a Dictionary. The dictionary contains a
I have a ListBox, and when I select an item in that ListBox I
I have a listbox that a user can select multiple values from . each
I have a ListBox control, that contains a few items that display attachments (files
I have listbox in my WP7 that uses the below DataTemplete to display the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.