views:

53

answers:

3

First of all, i'm a Graphic designer so please ignore if this programming question seems stupid ... I know this question could've been split into two or three smaller questions but since I'm really new to coding VB.NET it would've killed me trying to put together the stuff ...

Directory Structure: I have a directory structure as follows;

ad_folder
--folderA
--folderB
--folderC
--anotherFolder
--etcfolder
--afile.aspx
--anotherfile.gif
ad_code
--folderA
--folderB
--afile.aspx
--anotherfile.gif
ad_prep
--folderA
--etcfolder
--afile.aspx
--anotherfile.gif
ad_bin
--etcfolder
--afile.aspx
--anotherfile.gif
other Folder
files folder
assetsfolder
index.aspx
web.config
image.gif

Task at hand:

I want code in VB.NET to create javascript arrays of the folder contents that can then be used on the client end. I only need arrays for all folders contained in folders starting with ad_ and an array for all the base folders . like so:

var folders=["ad_folder","ad_code","ad_prep","ad_bin"];
var ad_folder=["folderA","folderB","folderC","anotherFolder","etcfolder"];
var ad_code=["folderA","folderB"];
var ad_prep=["folderA","etcfolder"];
var ad_bin=["etcfolder"];

Please note that I do not know the number of or the names of the folders, they can be different in different cases, I only have the root path. sorry for sounding stupid.

I'll appriciate any help anyone can provide ... I'm super new to programming, I've googled on how I can display folder contents in VB.net and the code worked but couldn't figure out how to create the arrays and display only folders within folders starting with "ad_".

Thankyou soooooo much ... :) ... If anyone needs any graphic design / photoshop help ... I'll be glad to ;) ... just let me know.



UPDATE : okay ... by googling I know :

  1. System.IO.DirectoryInfo and System.IO.FileInfo to be used for getting the folders.

  2. A literal control can be used to create javascript arrays in ASP.NET. These js arrays can then be used on the client side.

  3. The pseudo for what I want would be something like;

declare path
    if path exists and is not empty then
    ' get all folders starting with 'ad_'
    ' if folders starting with 'ad_' are > 0
    ' loop through all folders starting with 'ad_'
    ' ' if this folder exists and not empty
    ' ' get all folders within this folder
    ' ' create literal control for javascript array named 'this folder's name'
    ' create literal control for javascript array called 'folders' containing names of all folders starting with 'ad_'.
+1  A: 

Edit: I saw that you worked out how to include the parent folder array but I went ahead and updated my answer to include it. I also added in the sorting you asked for, though I'm not sure if it works just like you want or not, let me know.


I believe the code below will do what you want. The important line is the one that response writes GetJavaFolderArrays. Put this where you want the javascript arrays to output. In my example I put it inside of a javascript block, which made sense to me :)

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <script runat="server">
        Private dirArray As String = "var {0}=[{1}];"

        Protected Function GetJavaFolderArrays(ByVal RootPath As String) As String
            'get all folders starting with ad_'
            Dim adFolders() As String = Directory.GetDirectories(RootPath, "ad_*")

            'sort using numbers'
            Array.Sort(adFolders, New PathNumberSorter())

            'string builder to hold the output'
            Dim sb As New StringBuilder()

            'build parent folder list
            sb.AppendLine(GetADFolder("folders", adFolders))

            'loop through folders'
            For Each d As String In adFolders
                sb.AppendLine(GetADFolder(Path.GetFileName(d), Directory.GetDirectories(d)))
            Next

            'return the string builder'
            Return sb.ToString()
        End Function

        Protected Function GetADFolder(ByVal ParentName As String, ByVal cf() As String) As String
            'sort the array'
            Array.Sort(cf, New PathNumberSorter())

            'javascript array'
            Dim jarray As String = String.Empty

            'loop through folders'
            For Each d As String In cf
                jarray += String.Format("""{0}"",", Path.GetFileName(d))
            Next

            'remove extra ,'
            jarray = jarray.Trim(",")

            Dim jfinal As String = String.Format(dirArray, ParentName, jarray)

            Return jfinal
        End Function

        Friend Class PathNumberSorter
            Implements IComparer(Of String)

            'used for finding all numbers in string'
            Private pattern As String = "[0-9]+"

            Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
                'get number from x and y
                Dim xMatch As RegularExpressions.Match = Regex.Match(Path.GetFileName(x), pattern)
                Dim yMatch As RegularExpressions.Match = Regex.Match(Path.GetFileName(y), pattern)

                If xMatch.Success And yMatch.Success Then
                    Dim xInt As Integer = Convert.ToInt32(xMatch.Value)
                    Dim yInt As Integer = Convert.ToInt32(yMatch.Value)

                    Return xInt.CompareTo(yInt)
                Else
                    Return x.CompareTo(y)
                End If
            End Function
        End Class
    </script>
    <form id="form1" runat="server">
    <script type="text/javascript">
        <%=GetJavaFolderArrays(Server.MapPath("~/"))%>
    </script>
    <div>

    </div>
    </form>
</body>
</html>
Patricker
Wow .... awsome ... works like a charm ... thankyou thankyou thankyou !!! Can't believe the code looks so little compared to what I was working on .... really neat .... thank you soo much :D you made my day !Just one small thing ... how do I get another js array called adfolders with the names of all the parent folders (that is ... the folders starting with 'ad_') -- I'll be able to figure this one out though. thanks a bundle ... may sound stupid but let me know if there's anything I can help you with.
Norman
Is there a way to sort the array in a way that numbered folders appear in order .... currently they display like this; "1","10","2","3","4","5","6","7","8","9"It would be great if they could be like; "1","2","3","4","5","6","7","8","9","10"Thanks :)
Norman
OMG .... JAWDROPPER !! ... that's some neat chunk of code :) ... Can't wait to try it out ! ...Q: .. do you think a sorter can be built that sorts the arrays like Windows explorer in win7 / xp ... both alphabetically and numerically? for when there are numbers and alphabetical folder names also in one directory ... did the question make any sense ?Thank super much ... this would've taken me days to figure out ... thank you ! thank you ! thank you Patricker !
Norman
A: 

UPDATE:

This is a lousy attempt to add the parent folders ... Patricker has updated his answer to include the necessary code to create the parent folders. His solution is much neater / simpler / easier to maintain and definitely more compact !


Just added the following to the end of the first function and this creates the js array called adFolders with the names of all the parent folders (that is ... the folders starting with 'ad_')

Thank you Patricker !

          'create array for parent folders'
            Dim parray As String = String.Empty
            For Each d As String In adFolders
                parray +=enter code here String.Format("""{0}"",", Path.GetFileName(d))
            Next

            'remove extra ,'
            parray = parray.Trim(",")
            parray = String.Format(dirArray, "adFolders", parray)
            sb.AppendLine(parray)

            'return the string builder'
            Return sb.ToString()
Norman
A: 

Patricker,

I asked another question to fix the sorting ... by using code from the answer by Josh here;

http://stackoverflow.com/questions/3099581

I was able to sort all folder names like explorer in windows. Works great ! ...

Public Class nameSorter
    Implements IComparer(Of String)

    Declare Unicode Function StrCmpLogicalW Lib "shlwapi.dll" ( _
        ByVal s1 As String, _
        ByVal s2 As String) As Int32

    Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
        Return StrCmpLogicalW(x, y)
    End Function

End Class

this sorter above can be used to sort any array explorer style like so;

Array.Sort(cf, New nameSorter())

I'm not sure how it will behave in case the library is not available (like on win server 2003 or xp and below.) - would be nice if a check could be placed for library existence.

Thank you Patricker!

Thanks, Norman.

Norman