Loop through directory and sub directories

Posted on 2012-09-18
Medium Priority
Last Modified: 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
                    HeaderScripts.Append("<script type='text/javascript' src='~/Scripts/" & FI.Name & "'></script>")
                End If
            End If
        Next FI

        litScripts.Text = HeaderScripts.ToString

    End Sub
Question by:gogetsome
  • 3
LVL 23

Accepted Solution

apresto earned 2000 total points
ID: 38410117
Hi, check this out:


Replace *.* with *.js and adfd in the action to append to the stringbuilder
LVL 23

Assisted Solution

apresto earned 2000 total points
ID: 38410146
Here is another good one:


And here is the function:

Imports System.IO

Sub GetAllFiles(strPath As String)
	Dim objRoot As New DirectoryInfo(strPath)
	Dim objSubDir As DirectoryInfo()
	Dim objFile As FileInfo()

	If objRoot.Exists Then
		'if you don't want this to be recursive, remove this for loop
		For Each objSubDir In objRoot.GetDirectories

		'keep this for loop
		For Each objFile In objRoot.GetFiles
			[This is where you want to check for the .Js file and append]
	End If
End Sub

Open in new window


Author Closing Comment

ID: 38410298
Thank you for your assistance!
LVL 23

Expert Comment

ID: 38410669
Glad I could help

Happy coding

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

862 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