Search code examples
vb.netsystem.io.filesystem.io.directory

Iterate through a directory to get subfolders and certain files


I am working on a program that will move files to a database, text files to be exact. The user will have a starting directory and inside will multiple sub folders and files and so on. I want to go through each Folder and sub folder looking for the text files and add them accordingly to the database so it resembles a directory structure in a way. In the program the files are organized such as the folders are "Categories" that are displayed into a tree view.I am only adding the folder names(as Categories) that do contain text files and their subs and so forth. As well I need to figure out where to plug in the adding of the "Category". As of right now I am using a couple of listboxes for my output until I can get it all figured out.

    lstfiles.Items.Add(Dir)
    For Each file In System.IO.Directory.GetFiles(Dir)
        Dim fi As System.IO.FileInfo = New IO.FileInfo(file)
        If fi.Extension = ".txt" Then
            If lstRootFolderFiles.Items.Contains(file) = False Then
                lstfiles.Items.Add(file)
                AddFile(file, Gennumber(9999))
            End If
        End If
    Next

     For Each folder In System.IO.Directory.GetDirectories(Dir)
        lstfiles.Items.Add(folder)
        For Each file In System.IO.Directory.GetFiles(folder)
            Dim fi As System.IO.FileInfo = New IO.FileInfo(file)
            If fi.Extension = ".txt" Then
                If lstRootFolderFiles.Items.Contains(file) = False Then
                    lstfiles.Items.Add(file)
                End If
            End If
        Next
    Next

I have gotten so far as to iterate through the directory and get files but it returns folders that are empty. And I need to figure out where I need to put in my addcategory function. And also remember the last one that was added so they can be added as a subcategory.

I think I am making a big mess of it all, or over thinking the whole thing.

Any assistance or guidance would be appreciated.Thank you


Solution

  • The end result that I came up with was much different from my original posting, as it was my way of thinking of how it was going to work. I split up the two main functions. Retrieving the Folders and retrieving the files.

    DirEx is the Import Directory, CatID is the Tag of the selected Treenode where the folders are going to added in.

    Private Sub DirImport_GetSubDirectories(ByVal DirEx As String, ByVal CatID As Integer)
    
        Try
            Dim ClsData As New clsNoteData
            'Set the DatabaseFile Property of the class
            ClsData.Database = LoadedLibraryDatabase
            ' Get all subdirectories  
            Dim subdirectoryEntries() As String = Directory.GetDirectories(DirEx)
            ' Loop through them to see if they have any other subdirectories  
            Dim subdirectory As String
    
            For Each subdirectory In subdirectoryEntries
                'If the Directory is not Empty
                If Not Directory.GetFileSystemEntries(subdirectory).Length = 0 Then
                    Dim di As DirectoryInfo = New DirectoryInfo(subdirectory)
                    'Creating Random Number
                    Dim NewCatID As Integer = GenNumber(9999)
                    'Call for a new Sub Category
                    ClsData.NewSubCategoryNode(LoadedLibraryDatabase, NewCatID, CatID, di.Name, -1)
                    'Get files in the current Directory
                    DirImport_GetFiles(subdirectory, NewCatID)
                    'Get the next set of Subfolders
                    DirImport_GetSubDirectories(subdirectory, NewCatID)
                End If
    
            Next
        Catch ex As Exception
    
        End Try
    End Sub
     Private Sub DirImport_GetFiles(ByVal DirEx As String, ByVal CatID As Integer)
      
        Dim Files() As String = Directory.GetFiles(DirEx, "*.txt")
        Dim file As String
        For Each file In Files
            Dim clsNoteData As New clsNoteData
            Dim fi As FileInfo = New FileInfo(file)
            clsNoteData.Database = LoadedLibraryDatabase
            clsNoteData.NewNote_ID = GenNumber(99999)
            clsNoteData.NewNote_CatID = CatID
            clsNoteData.NewNote_Title = Path.GetFileNameWithoutExtension(file)
            clsNoteData.NewNote(False, file)
        Next
    

    End sub

    So here it is for anyone who may want to do something similar.