Subdirectories VB.Net Exercise - Visual Basic Programming Course


 Exercise

Subdirectories

Objetive

Create a program to store the files that are located in a particular folder and its subfolders.

Then, it will ask the user which text to search and it will display the files containing that text in their name.

Program will end when the user enters an empty search string.

Code

Imports System
Imports System.IO

Class Subdirectories
    Private Shared Sub Main()
        Try
            Dim text As String = ""
            Console.Write("Enter a directory for search: ")
            text = Console.ReadLine()

            While text <> ""
                Dim directory As DirectoryInfo = New DirectoryInfo(text)
                Dim files As FileInfo() = directory.GetFiles("*.*")
                Dim directories As DirectoryInfo() = directory.GetDirectories()
                Dim i As Integer = 0

                While i < files.Length
                    Console.WriteLine((CType(files(i), FileInfo)).FullName)
                    i += 1
                End While

                For i = 0 To directories.Length - 1
                    Console.WriteLine((CType(directories(i), DirectoryInfo)).FullName)
                Next

                Console.Write(vbLf & "Enter a directory for search: ")
                text = Console.ReadLine()
            End While

        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        End Try
    End Sub
End Class


Juan A. Ripoll - Systems Tutorials and Programming Courses © 2024 All rights reserved.  Legal Conditions.