Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Subfolders to the Xth Level

Posted on 2013-11-03
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Accepted Solution

AlexPace earned 668 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 58

Assisted Solution

by:Bill Prew
Bill Prew earned 668 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 30

Assisted Solution

anarki_jimbel earned 664 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

636 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