Solved

Subfolders to the Xth Level

Posted on 2013-11-03
6
304 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 16

Accepted Solution

by:
AlexPace earned 167 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 53

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
      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 29

Assisted Solution

by:anarki_jimbel
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
            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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to compare ms sql hashbytes results within vb6 5 73
MsgBox 4 59
Create Files based on Cell Values in a Range in Excel 12 39
how to Classify of email (pdf attachments) 7 36
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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