-1

Ok, I'm trying to figure the best way to verify the contents of a folder based on another count via a listbox. Let me further explain.

Here is my current code to count the number of PDFs in two different locations and total them together for a grand total.

    'counts test1 pdfs
    Dim f As String, c As Long
    f = Dir$("\\Test1\PDFs\*.pdf")
    Do While Len(f) <> 0
        c = c + 1
        f = Dir$()
    Loop

    'counts test2 pdfs
    Dim n As String, d As Long
    n = Dir$("\\Test2\PDFs\*.pdf")
    Do While Len(f) <> 0
        d = d + 1
        n = Dir$()
    Loop

    GtotalPDFs = c + d

Here is my current code to count files I've selected in a listbox.

    'adds temp1 files
    Dim sum1 As Double
    For Each item As String In Me.ListBox6.Items
        sum1 += Double.Parse(item)
    Next

    'adds temp2 files
    Dim sum2 As Double
    For Each item As String In Me.ListBox7.Items
        sum2 += Double.Parse(item)
    Next

    'adds temp3 files
    Dim sum3 As Double
    For Each item As String In Me.ListBox8.Items
        sum3 += Double.Parse(item)
    Next

    'adds all files together to get a grand total
    Gtotal = sum1 + sum2 + sum3

I have another process before this that will create the PDF's based on the files listed in the listbox.

What I am having trouble with is verifying that the PDFs that are created in the Test1 and Test2 folders equal the counts from the listboxes. This count needs to match before running the next process. I'm kind looking for wait or loop until both counts match, again before running the next process.

Any suggestions?

Muhnamana
  • 1,014
  • 13
  • 34
  • 57
  • 1
    if you did set listbox `SelectionMode` to `multiple`, you can use `GetSelectedIndices().Count()` to know the number of selected items (this gives you indices). – Minus Jun 05 '12 at 13:10
  • I already know my listbox counts. The process that creates the PDFs takes some time (I'm creating a few hundred at a time). What I dont want is the next process to run until the PDFs that are created equal the count from the listboxes. – Muhnamana Jun 05 '12 at 13:18

2 Answers2

1

I'd be happy to help, but you are going to have to further explain what you are trying to do and what your problem is. Right now, it's not very clear what you need. However, to get you started, there are some definite improvements that can be made to your code.

First, never use Dir and Len. Those methods are only there for backwards compatibility with VB6, and they weren't even good programing practice to use in VB6! Use the objects in the System.IO namespace, such as:

Dim count1 As Integer = Directory.GetFiles("\\Test1\PDFs", "*.pdf").Length
Dim count2 As Integer = Directory.GetFiles("\\Test2\PDFs", "*.pdf").Length

Second, why are you using doubles in the second code example? If they are simple file counts, then you should be using Integers, not Doubles. However, it's not clear at all what you are doing here. The only whay the Double.Parse method will work in this case is if each item in the list contained a number. But in your description, you talk about the lists as if they contain file names.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • The listboxes show a set of files a users has selected, along with a line count to verify the quantity. I have a process that will create a PDF based on those counts to a directory. This process is not quick and takes some time. I'm trying to figure out how to wait until the PDFs in the directories equal the counts from the listboxes before continuing the next step. Which in my cause will zip those PDFs. – Muhnamana Jun 05 '12 at 13:24
  • 1
    @Muhnamana I'm still completely confused as to what your problem is. If you already know how you count the files, and you already know how to sum the totals in the list boxes, and you want to see if they are the same, all you need is a simple If statement like `If TotalFiles = ListBoxSum Then`. I'm sure that's not what you're asking, though. – Steven Doggart Jun 05 '12 at 13:29
  • Maybe...because I'm been looking at this for days. The If statement I could use but can I loop through to count, to keep checking it and once they equal, move onto the next step? – Muhnamana Jun 05 '12 at 13:36
  • 1
    @Muhnamana It's never a good idea to stay in a loop like that on the UI thread. Use a Timer instead, if possible. If you are using WinForms, you can just drop a timer control onto your form, set the interval, and then add code to the Elapsed event of the timer control to check the counts. – Steven Doggart Jun 05 '12 at 13:38
  • Any reason I cant use the wildcard in your code above? I get a illegal characters in path. – Muhnamana Jun 05 '12 at 14:08
  • I see why your code is easier than what I did..geesh much cleaner. Thanks again! – Muhnamana Jun 05 '12 at 14:15
1
Imports System.IO 

Dim PDFFileCount As Integer = 0
Dim ListboxCount As Integer = 0
While Not (PDFFileCount > 0 And PDFFileCount = ListboxCount)
  PDFFileCount = Directory.GetFiles("\\Test1\PDFs", "*.pdf").Count + _
                 Directory.GetFiles("\\Test2\PDFs", "*.pdf").Count
  ListboxCount = ListBox6.SelectedItems.Count + ListBox7.SelectedItems.Count + _
                 ListBox8.SelectedItems.Count
  Application.DoEvents()
End While
Holger Brandt
  • 4,324
  • 1
  • 20
  • 35
  • I think this is exactly what I am looking for. I appreciate your assistance. – Muhnamana Jun 05 '12 at 13:55
  • 1
    @Muhnamana, Happy to help. As SteveDog pointed out, it's not good to have this on the GUI thread hence I added to Application.DoEvents. However, Application.DoEvents is not a good programming technique either but I wanted to keep things straightforward for you. – Holger Brandt Jun 05 '12 at 14:00
  • Yeah I'm by far still fairly new to this, hence my awful explanation of what I am trying to do..haha! But yes, straight forward is best for me. – Muhnamana Jun 05 '12 at 14:05