好的,我正在尝试通过列表框找出基于其他计数来验证文件夹内容的最佳方法。让我进一步解释一下。
以下是我目前的代码,用于统计两个不同位置的PDF数量,并将它们合计为总数。
'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
这是我当前的代码,用于计算我在列表框中选择的文件。
'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
我之前有另一个过程,它将根据列表框中列出的文件创建PDF。
我遇到的问题是验证在Test1和Test2文件夹中创建的PDF是否与列表框中的计数相等。在运行下一个进程之前,此计数需要匹配。我正在寻找等待或循环,直到两个计数匹配,再次运行下一个过程之前。
有什么建议吗?
答案 0 :(得分: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方法在这种情况下唯一有效的方法是列表中的每个项目都包含一个数字。但在您的描述中,您可以将列表视为包含文件名。
答案 1 :(得分: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