|
回复 sgrshh29 的帖子
这一节是文件夹导入导出
六、导入导出文件夹
这是难度比较大的一节内容。
操作过程是:
选择一个节点,点击菜单按钮“导入文件夹”,在选择文件夹对话框中把选中的文件夹连同它下面的所有文件夹添加到节点下,顺序、,名称不变
如果没有选择节点,则把这个文件夹作为一个新的根节点导入,它下面的文件夹顺序不变、不变。
1、选择文件夹对话框代码
Function getFolderName() As String
Dim dlgFolder As Variant
Set dlgFolder = Application.FileDialog(4)
With dlgFolder
.title = "请选择文件夹"
.AllowMultiSelect = False
.Filters.Clear
.Show
End With
If dlgFolder.SelectedItems.count > 0 Then
getFolderName = dlgFolder.SelectedItems(1)
Else
getFolderName = ""
End If
Set dlgFolder = Nothing
DoEvents
End Function
2、创建对filesystemobject和dictionary二个对象的引用
Public objFso As Object
Public objDic As Object
Sub CreateFsoDic()
Set objDic = CreateObject("scripting.dictionary")
Set objFso = CreateObject("scripting.filesystemobject")
End Sub
Sub RemoveFsoDic()
objDic.RemoveAll
Set objDic = Nothing
Set objFso = Nothing
End Sub
3、添加一个全局变量
Public lngGid AS Long
4、为"导入文件夹"菜单按钮添加事件过程
Sub mImportFolder()
Dim strGetFolderPath As String
strGetFolderPath = getFolderName
If strGetFolderPath = "" Then Exit Sub
Set objTree = Forms("frmMain").Controls("treeview0")
pnlEditText "当前操作:正在保存文件夹..."
CreateADOCnnRs
CreateFsoDic
If objTree.SelectedItem Is Nothing Then
strKey = "T" & Nz(DMax("id", "tblTreeview")) + 1
SaveFolderFile strGetFolderPath
Else
strKey = objTree.SelectedItem.Key
lngGid = Mid(objTree.SelectedItem.Key, 2)
SaveFolderFile strGetFolderPath
End If
RemoveADOCnnRs
RemoveFsoDic
pnlEditText "当前操作:保存文件夹完成"
加载Treeview Forms("frmMain").Controls("Treeview0")
objTree.Nodes(strKey).Selected = True
objTree.Nodes(strKey).Expanded = True
End Sub
5、导入文件夹模块,这是一个递归函数
Dim lngIDp As Long
Function SaveFolderFile(ByVal strPath As String)
On Error Resume Next
Dim lngSubP As Long
Dim strSubP() As String
Dim strParent As String
Dim objFolder As Object
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
If Len(strPath) - Len(Replace(strPath, "\", "")) = 1 Then
strParent = objFso.GetFolder(strPath)
Else
strParent = objFso.GetFolder(strPath).ParentFolder
End If
If Right(strParent, 1) <> "\" Then strParent = strParent & "\"
If objDic.Exists(strParent) Then
lngGid = objDic(strParent)
End If
If DCount("id", "tblTreeview") > 0 Then
lngIDp = DMax("id", "tblTreeview") + 1
Else
lngIDp = lngIDp + 1
End If
objDic.Add strPath, lngIDp
If Len(strPath) - Len(Replace(strPath, "\", "")) = 1 Then
AddFolder lngIDp, lngGid, objFso.getdriveName(objFso.getdrive(strPath)), _
objFso.getdrive(strPath).totalsize
Else
AddFolder lngIDp, lngGid, objFso.GetFolder(strPath).Name, _
objFso.GetFolder(strPath).Size, objFso.GetFolder(strPath).DateLastModified
End If
For Each objFolder In objFso.GetFolder(strPath).SubFolders
Select Case objFolder.Attributes
Case 16, 17, 48
lngSubP = lngSubP + 1
ReDim Preserve strSubP(1 To lngSubP)
strSubP(lngSubP) = objFolder.Path
End Select
Next
For lngSubP = 1 To lngSubP
SaveFolderFile strSubP(lngSubP)
Next lngSubP
End Function
6、将记录追加到数据表的过程,这里用到了sql语句的insert语句
Sub AddFolder(ByVal lngIDp As Long, ByVal lngGid As Long, ByVal strPName As String, _
ByVal lngPsize As Double, Optional ByVal datPDLModi As Date)
Dim strSql As String
strSql = "insert into tblTreeview (id, gid, pname, psize, pdate) select " & """" & lngIDp & """" & "," & """" & lngGid _
& """" & "," & """" & strPName & """" & "," & """" & lngPsize & """" & "," & """" & datPDLModi & """"
CurrentDb.Execute (strSql)
End Sub
七、在硬盘指定的位置将节点按原样导出为文件夹,实际就是上一节的反向操作
1、为"导出文件夹"菜单按钮添加事件过程
Sub mExportFolder()
Set objTree = Forms("frmMain").Controls("treeview0")
If objTree.SelectedItem Is Nothing Then Exit Sub
pnlEditText "当前操作:正在将文件夹保存到硬盘..."
ExportNode objTree.SelectedItem, objTree.SelectedItem.Text
pnlEditText "当前操作:文件夹保存到硬盘完成"
End Sub
2、导出过程
Sub ExportNode(ByVal objNode As Node, ByVal strNodText As String)
On Error Resume Next
Dim strPath As String
strPath = getFolderName
If strPath <> "" Then
CreateADOCnnRs
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
ExportSubNode objNode, strPath, strNodText
RemoveADOCnnRs
Else
MsgBox "没有选择保存位置。", vbInformation, "Information"
Exit Sub
End If
End Sub
3、递归过程
Sub ExportSubNode(ByVal objNode As Node, ByVal strPath As String, ByVal strNodText As String) '节点递归
Dim i As Integer
Dim strFolder As String
Dim strSubFld As String
Dim intNodChildren As Integer
Dim nodChild As Node
intNodChildren = objNode.Children
strSubFld = Mid(objNode.FullPath, InStr(objNode.FullPath, strNodText))
strFolder = strPath & strSubFld & "\"
If Dir(strFolder, vbDirectory) = "" Then
MkDir strFolder
Else
If MsgBox("文件夹: " & strFolder & Chr(10) & Chr(13) & " 已经存在,是否继续?", _
vbInformation + vbYesNo, "Information") = vbNo Then End
End If
If intNodChildren > 0 Then
Set nodChild = objNode.Child
For i = 1 To intNodChildren
ExportSubNode nodChild, strPath, strNodText
Set nodChild = nodChild.Next
Next i
End If
End Sub
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|