-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?

4

2 回答 2

1

我很乐意提供帮助,但您将不得不进一步解释您正在尝试做什么以及您的问题是什么。现在,还不是很清楚你需要什么。但是,为了让您开始,可以对您的代码进行一些明确的改进。

首先,永远不要使用 Dir 和 Len。这些方法只是为了向后兼容 VB6,它们甚至不是在 VB6 中使用的良好编程实践!使用 System.IO 命名空间中的对象,例如:

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

其次,为什么在第二个代码示例中使用双精度数?如果它们是简单的文件计数,那么您应该使用整数,而不是双精度数。但是,根本不清楚您在这里做什么。在这种情况下,Double.Parse 方法起作用的唯一原因是列表中的每个项目都包含一个数字。但是在您的描述中,您谈论列表时就好像它们包含文件名一样。

于 2012-06-05T13:16:27.000 回答
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
于 2012-06-05T13:25:32.417 回答