VB6 在执行代码后检查 se zip 是否为空
VB6 check to se if zip is empty after execute a code
这是我的代码,执行后它开始从 treeview2 中删除不匹配的项目。
如果某些 zip 文件不包含匹配的文件,则 zip 文件的内容将被删除,但 zip 仍为 1k 大小且为空。
我可以将 zip 移动到 zip 路径中新创建的文件夹,并保持内容不变并继续前进,而不是留下死空的 zip。
Private Sub Command9_Click()
Dim objNode1 As Node
Dim objNode2 As Node
Dim objMatchNode As Node
Dim objChildNode1 As Node
Dim objChildNode2 As Node
Dim iCounter1 As Integer
Dim iCounter2 As Integer
Dim fFound As Boolean
On Error Resume Next
For Each objNode1 In TreeView2.Nodes
' Find matching node in Treeview2
For Each objNode2 In TreeView1.Nodes
If objNode2.Text = objNode1.Text Then
' Match found
Set objMatchNode = objNode2
Exit For
End If
Next
If Not objMatchNode Is Nothing Then
' Check all children
If objNode1.Children > 0 Then
' Get first Child
Set objChildNode1 = objNode1.Child
' Loop through all children
For iCounter1 = 1 To objNode1.Children
'If objChildNode1.Image = 3 And objNode1.Image = 9 Then
' Check if it already exists in Treeview2
If objMatchNode.Children > 0 Then
' Get first Child
Set objChildNode2 = objMatchNode.Child
' Set Found flag to False
fFound = False
' Loop through all children
For iCounter2 = 1 To objMatchNode.Children
' Check for match
If objChildNode2.Text = objChildNode1.Text Then
fFound = True
Exit For
End If
' Get next node
Set objChildNode2 = objChildNode2.Next
DoEvents
Next
If fFound Then
' Add to Treeview2
'TreeView2.Nodes.Add objMatchNode.Key, tvwChild, objChildNode1.Key, objChildNode1.Text, 3
Else
DeleteFileFromArchive objChildNode1.Text, "C:\Users\sarah\Desktop\rom test\" & objNode2.Text
End If
End If
' End If
' Get next node
Set objChildNode1 = objChildNode1.Next
DoEvents
Next
End If
End If
Next
End Sub
下面的代码会找到空的节点并删除它们。您可以在此处添加代码以删除实际的 Zip 文件,其中显示 "Delete Zip":
Private Sub DeleteFromTreeView(ByRef p_objTreeView As TreeView)
Dim objNode As Node
Dim fDelete As Boolean
Dim iDeleteIndex As Integer
Dim sDeleteName As String
' Get first node from TreeView
Set objNode = p_objTreeView.Nodes(1)
Do While Not objNode Is Nothing
' Set Delete flag to false
fDelete = False
' Check if node has children, otherwise delete file
If objNode.Children = 0 Then
fDelete = True
iDeleteIndex = objNode.Index
sDeleteName = objNode.Text
End If
' Go to next sibling
Set objNode = GetNextSibling(p_objTreeView, objNode)
If fDelete Then
' Delete Zip
p_objTreeView.Nodes.Remove iDeleteIndex
End If
Loop
End Sub
您可以在现有代码之后 运行 在 TreeView 上添加此代码。 sDeleteName
将包含您要删除的 Zip 的名称,只需添加一些代码即可使用以下内容删除文件:
Sub DeleteFile(p_sFilePath)
Dim objFSO As New FileSystemObject
If objFSO.FileExists(p_sFilePath) Then objFSO.DeleteFile p_sFilePath
End Sub
此 Sub 使用 FileSystemObject,因此请确保在您的项目中添加对 Microsoft Scripting Runtime 的引用。
您还需要项目中可能已有的以下帮助函数:
Function GetNextSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Node
If HasSibling(p_objTreeView, p_objNode) Then
Set GetNextSibling = p_objTreeView.Nodes(GetNextSiblingIndex(p_objNode))
Else
Set GetNextSibling = Nothing
End If
End Function
Function HasSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Boolean
HasSibling = Not (p_objNode.LastSibling Is p_objNode)
End Function
Function GetNextSiblingIndex(ByRef p_objNode As Node) As Integer
With p_objNode
GetNextSiblingIndex = .Index + .Children + 1
End With
End Function
这是我的代码,执行后它开始从 treeview2 中删除不匹配的项目。
如果某些 zip 文件不包含匹配的文件,则 zip 文件的内容将被删除,但 zip 仍为 1k 大小且为空。
我可以将 zip 移动到 zip 路径中新创建的文件夹,并保持内容不变并继续前进,而不是留下死空的 zip。
Private Sub Command9_Click()
Dim objNode1 As Node
Dim objNode2 As Node
Dim objMatchNode As Node
Dim objChildNode1 As Node
Dim objChildNode2 As Node
Dim iCounter1 As Integer
Dim iCounter2 As Integer
Dim fFound As Boolean
On Error Resume Next
For Each objNode1 In TreeView2.Nodes
' Find matching node in Treeview2
For Each objNode2 In TreeView1.Nodes
If objNode2.Text = objNode1.Text Then
' Match found
Set objMatchNode = objNode2
Exit For
End If
Next
If Not objMatchNode Is Nothing Then
' Check all children
If objNode1.Children > 0 Then
' Get first Child
Set objChildNode1 = objNode1.Child
' Loop through all children
For iCounter1 = 1 To objNode1.Children
'If objChildNode1.Image = 3 And objNode1.Image = 9 Then
' Check if it already exists in Treeview2
If objMatchNode.Children > 0 Then
' Get first Child
Set objChildNode2 = objMatchNode.Child
' Set Found flag to False
fFound = False
' Loop through all children
For iCounter2 = 1 To objMatchNode.Children
' Check for match
If objChildNode2.Text = objChildNode1.Text Then
fFound = True
Exit For
End If
' Get next node
Set objChildNode2 = objChildNode2.Next
DoEvents
Next
If fFound Then
' Add to Treeview2
'TreeView2.Nodes.Add objMatchNode.Key, tvwChild, objChildNode1.Key, objChildNode1.Text, 3
Else
DeleteFileFromArchive objChildNode1.Text, "C:\Users\sarah\Desktop\rom test\" & objNode2.Text
End If
End If
' End If
' Get next node
Set objChildNode1 = objChildNode1.Next
DoEvents
Next
End If
End If
Next
End Sub
下面的代码会找到空的节点并删除它们。您可以在此处添加代码以删除实际的 Zip 文件,其中显示 "Delete Zip":
Private Sub DeleteFromTreeView(ByRef p_objTreeView As TreeView)
Dim objNode As Node
Dim fDelete As Boolean
Dim iDeleteIndex As Integer
Dim sDeleteName As String
' Get first node from TreeView
Set objNode = p_objTreeView.Nodes(1)
Do While Not objNode Is Nothing
' Set Delete flag to false
fDelete = False
' Check if node has children, otherwise delete file
If objNode.Children = 0 Then
fDelete = True
iDeleteIndex = objNode.Index
sDeleteName = objNode.Text
End If
' Go to next sibling
Set objNode = GetNextSibling(p_objTreeView, objNode)
If fDelete Then
' Delete Zip
p_objTreeView.Nodes.Remove iDeleteIndex
End If
Loop
End Sub
您可以在现有代码之后 运行 在 TreeView 上添加此代码。 sDeleteName
将包含您要删除的 Zip 的名称,只需添加一些代码即可使用以下内容删除文件:
Sub DeleteFile(p_sFilePath)
Dim objFSO As New FileSystemObject
If objFSO.FileExists(p_sFilePath) Then objFSO.DeleteFile p_sFilePath
End Sub
此 Sub 使用 FileSystemObject,因此请确保在您的项目中添加对 Microsoft Scripting Runtime 的引用。
您还需要项目中可能已有的以下帮助函数:
Function GetNextSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Node
If HasSibling(p_objTreeView, p_objNode) Then
Set GetNextSibling = p_objTreeView.Nodes(GetNextSiblingIndex(p_objNode))
Else
Set GetNextSibling = Nothing
End If
End Function
Function HasSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Boolean
HasSibling = Not (p_objNode.LastSibling Is p_objNode)
End Function
Function GetNextSiblingIndex(ByRef p_objNode As Node) As Integer
With p_objNode
GetNextSiblingIndex = .Index + .Children + 1
End With
End Function