Loop through directory and sub directories

Posted on 2012-09-18
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
    LVL 23

    Accepted Solution

    Hi, check this out:

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

    Assisted Solution

    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

    Thank you for your assistance!
    LVL 23

    Expert Comment

    Glad I could help

    Happy coding

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Flash ( has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    This video discusses moving either the default database or any database to a new volume.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now