Loop through directory and sub directories
Posted on 2012-09-18
Hello, I'm attempting to loop through a directory that also contains sub directories. I'm looking for any .js files that are in those directories and then adding them to a stringbuilder. After I loop through the directories I populate a literal control in a page head with the .js files.
Currently, I'm only getting the .js files that are in the top directory. How can I modify my code below to parse the sub directories as well?
Private Sub GetHeaderScripts()
Dim HeaderScripts As New StringBuilder
Dim Path As String = ("~/Scripts/")
Dim dirInfo As New DirectoryInfo(Server.MapPath(Path))
Dim fileInfo As FileInfo() = dirInfo.GetFiles("*.*", SearchOption.AllDirectories)
Dim myDI As New DirectoryInfo(Server.MapPath("~/Scripts/"))
Dim myFI As FileInfo() = myDI.GetFiles()
Dim FI As FileInfo
For Each FI In myFI
If HeaderScripts.ToString.Contains(FI.Name) Then
If FI.Name.EndsWith(".js") Then
litScripts.Text = HeaderScripts.ToString