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.
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 54

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

821 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