VB6如何检查子文件夹是否存在

发布于 2024-12-04 15:01:12 字数 572 浏览 0 评论 0原文

我几十年来一直在寻找这个,但没有成功。 我需要查明我指定的名称的子文件夹是否存在。

For i = 0 To 3 'got 4 different loc to check sub folders
    Set f = fso.GetFolder(backupdir(i)) 
    Set sf = f.SubFolders
    For Each fr In sf 'for each folder in sub folder
        Do Until fr = "" Or fr = Null 
            If fso.FolderExists(fr.SubFolders) Then 
            'if more sub folders exist i wanna make sure
            'that i can get their subfolders too
            'till there is no sub folder left..
                sf = fr
            End If
        Loop
    Next fr
Next i

Ive been searching for this for like decades with no success.
I need to find out if a subdfolder that i ve given the name is exist..

For i = 0 To 3 'got 4 different loc to check sub folders
    Set f = fso.GetFolder(backupdir(i)) 
    Set sf = f.SubFolders
    For Each fr In sf 'for each folder in sub folder
        Do Until fr = "" Or fr = Null 
            If fso.FolderExists(fr.SubFolders) Then 
            'if more sub folders exist i wanna make sure
            'that i can get their subfolders too
            'till there is no sub folder left..
                sf = fr
            End If
        Loop
    Next fr
Next i

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福还没到 2024-12-11 15:01:12

诸如遍历文件夹的文件夹中的文件夹之类的操作称为递归

FindFirstFile:使用文件夹掩码递归搜索文件夹(最少代码)示例 此处展示了如何使用 Windows API 快速完成此操作。

Actions like traversing the folders-of-a-folder-of-a-folder... is called recursing.

The FindFirstFile: Recursive Search for Folders Using a Folder Mask (minimal code) example here shows how to do this quickly with the windows API.

山川志 2024-12-11 15:01:12

检查此代码。

Dim FolderList As String

Private Sub SubCheck1(folderToCheck As String)
   Dim fso, f, f1, s, sf
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderToCheck)
   Set sf = f.SubFolders
   For Each f1 In sf
      FolderList = FolderList & "|" & f1.Name
      Call SubCheck1(f1.Path)
   Next
End Sub

Private Sub Form_Load()
   Call SubCheck1("c:\folderToSearch")
   Debug.Print FolderList
End Sub

此代码获取指定目录(本例中为 c:\folderToSearch)中的所有子文件夹,并将它们写入带有“|”的字符串FolderList 中。分隔符。

您还可以使用数组而不是用“|”分隔文件夹名称。
或者您可以在 Form_Load 中的 Debug.Print FolderList 行后添加以下代码来获取数组:

myArray = split(FolderList, "|")

Check this code.

Dim FolderList As String

Private Sub SubCheck1(folderToCheck As String)
   Dim fso, f, f1, s, sf
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderToCheck)
   Set sf = f.SubFolders
   For Each f1 In sf
      FolderList = FolderList & "|" & f1.Name
      Call SubCheck1(f1.Path)
   Next
End Sub

Private Sub Form_Load()
   Call SubCheck1("c:\folderToSearch")
   Debug.Print FolderList
End Sub

This code gets all the subfolders in the specified directory(c:\folderToSearch in this case) and writes them into the string FolderList with a "|" seperator.

You can also use an array instead of separating folder names with a "|".
Or you can add the code below after the Debug.Print FolderList line in Form_Load to get an array:

myArray = split(FolderList, "|")
趁微风不噪 2024-12-11 15:01:12

您可以使用集合和标准 Dir 函数在 20 行代码中“递归”子文件夹,如下所示

Private Sub Command1_Click()
    Dim vElem           As Variant

    For Each vElem In pvGetFolders("C:\TEMP")
        Debug.Print vElem
    Next
End Sub

Private Function pvGetFolders(ByVal sRoot As String) As Collection
    Dim lIdx            As Long
    Dim sFile           As String

    Set pvGetFolders = New Collection
    pvGetFolders.Add sRoot
    Do While lIdx < pvGetFolders.Count
        lIdx = lIdx + 1
        sFile = Dir(pvGetFolders.Item(lIdx) & "\*.*", vbDirectory)
        Do While LenB(sFile) <> 0
            If sFile <> "." And sFile <> ".." Then
                sFile = pvGetFolders.Item(lIdx) & "\" & sFile
                If (GetAttr(sFile) And vbDirectory) <> 0 Then
                    pvGetFolders.Add sFile
                End If
            End If
            sFile = Dir
        Loop
    Loop
    pvGetFolders.Remove 1
End Function

You can "recurse" subfolders with a collection and standard Dir function in 20 lines of code like this

Private Sub Command1_Click()
    Dim vElem           As Variant

    For Each vElem In pvGetFolders("C:\TEMP")
        Debug.Print vElem
    Next
End Sub

Private Function pvGetFolders(ByVal sRoot As String) As Collection
    Dim lIdx            As Long
    Dim sFile           As String

    Set pvGetFolders = New Collection
    pvGetFolders.Add sRoot
    Do While lIdx < pvGetFolders.Count
        lIdx = lIdx + 1
        sFile = Dir(pvGetFolders.Item(lIdx) & "\*.*", vbDirectory)
        Do While LenB(sFile) <> 0
            If sFile <> "." And sFile <> ".." Then
                sFile = pvGetFolders.Item(lIdx) & "\" & sFile
                If (GetAttr(sFile) And vbDirectory) <> 0 Then
                    pvGetFolders.Add sFile
                End If
            End If
            sFile = Dir
        Loop
    Loop
    pvGetFolders.Remove 1
End Function
飘过的浮云 2024-12-11 15:01:12

您可以使用 WMI。你可以在CIM_Directory中搜索,我相信字段名称是“name”,
如果它包含您要查找的文件夹或部分文件夹名称,请返回全名。

You could use WMI. You could search in CIM_Directory, I believe the field name is "name",
and if it contains the folder or partially the foldername you are looking for, return the full name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文