?
Solved

Subfolders to the Xth Level

Posted on 2013-11-03
6
Medium Priority
?
318 Views
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
0
Comment
Question by:APD_Toronto
[X]
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
6 Comments
 
LVL 16

Expert Comment

by:AlexPace
ID: 39620620
Scan it for what?
0
 

Author Comment

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

Expert Comment

by:AlexPace
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.
0
Independent Software Vendors: 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

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

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28260059.html

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

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
      Next

      ' 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
          Next
      End If

   End If
End Sub

Open in new window

~bp
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
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
            Debug.WriteLine(dir.FullName)
        Next

    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
        Next
    End Sub

Open in new window

0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. 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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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
Course of the Month11 days, 22 hours left to enroll

752 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