Subfolders to the Xth Level

Posted on 2013-11-03
Last Modified: 2013-11-04
Hello Experts,

I have a folder "D:\Client FTP Folders\" (spaces included) and I am wondering how can I scan its sub folders, sub-sub folders, sub-sub-sub folders, etc, to the Xth level, where X is unknown?

Thank you
Question by:APD_Toronto
LVL 16

Expert Comment

ID: 39620620
Scan it for what?

Author Comment

ID: 39620622
Each folder, sub folder, sub-sub ... I need to scan for *.txt and get it's content.
LVL 16

Expert Comment

ID: 39620628
Write a recursive function that uses GetExtensionName to identify the txt files or, since I notice this is in a folder named FTP client, you could use a scriptable FTP client like Robo-FTP to get only the files that match your name, size or date criteria in the first place.
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

LVL 16

Accepted Solution

AlexPace earned 167 total points
ID: 39620631
Here is a thread I found using the search on this site for recursive vbscript subfolder:

... if that doesn't work punch that into an internet search engine...  recursive is the key word for your scenario.
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 167 total points
ID: 39620693
Here's a modification to my post to a prior question you asked, adding level checking.  If you don't want the extension or date checking let me know and we can remove that.

' Define the folder to scan, and the age of files to locate
strBaseDir = "D:\Client FTP Folders"
datBefore = CDate("11/03/2013 7:32:24 PM")
intLevels = 2

' Create a dictionary to hold the list of extensions to list
Set dicExt = CreateObject("Scripting.Dictionary")
dicExt.CompareMode = vbTextCompare
dicExt.Add "ord", 0

' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Look for files
FindFiles objFSO.GetFolder(strBaseDir), 0

' Subroutine (recursive) to search for files to delete
Sub FindFiles(objFolder, ByVal intLevel)
   On Error Resume Next

   intLevel = intLevel + 1
   If intLevel <= intLevels Then

      ' Look at all files in this folder
      For Each objFile In objFolder.Files
          ' Make sure it's one of the extensions we want to delete
          If dicExt.Exists(objFSO.GetExtensionName(objFile.Path)) Then
             ' See if it's old enough to display
             If objFile.DateCreated < datBefore Then
                WScript.StdOut.WriteLine objFile.Path & ", " & objFile.DateCreated
             End If
          End If

      ' See if we were able to access this folder, if not don't recurse into it
      If Err.Number = 0 Then
          ' Recursively drill down into subfolder
          For Each objSubFolder In objFolder.SubFolders
              FindFiles objSubFolder, intLevel
      End If

   End If
End Sub

Open in new window

LVL 29

Assisted Solution

anarki_jimbel earned 166 total points
ID: 39620742
Yes, this is also my modified code from another question. As AlexPace told - the best way is to use recursive method. It is very simple:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim path As String = "C:\\temp"
        Dim topdir As New IO.DirectoryInfo(path)
        Dim dirList As New List(Of DirectoryInfo)
        findSubfoldersRecursively(topdir, dirList)

        For Each dir As DirectoryInfo In dirList

    End Sub

    Sub findSubfoldersRecursively(ByVal dir As IO.DirectoryInfo, ByVal dirList As List(Of DirectoryInfo))
        Dim dirs() As DirectoryInfo = dir.GetDirectories
        For Each adir As DirectoryInfo In dirs
            dirList.Add(adir) ' add to a list
            findSubfoldersRecursively(adir, dirList) ' and check this subfolder for subfolders
    End Sub

Open in new window


Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now