4
\$\begingroup\$

Follow-up question: Display all files in a folder (object) along with nested subdirectories part 2

Task: Given a main directory/folder, list all the files from it and if this directory have other nested sub-directories, list files from them also.

Background history: short story long - I got an internship, no past experience with asp.net-mcv-5, a nuisance of an assignment (Yet fulfilling), and now I want to optimize my code, giving the content is to be used for our intern job portal.

Path to a functional solution: In my quest for finding a solution have I looked at various recursive programs writing in other languages. However, they all made use of methods and after further research, I found out about @funtions in razor. It came to my conclusion, that people have different opinions on when, and if ever they should be used. That lead me to choose a different route, to finding a solution. I decided to go with Stacks inorder to store a collection of previous sub-directories, as I worked my way down each individual folder to display their content.

I would greatly appreciate feedback. And should there be any problems with my post regarding community rules, let me know.

Ps. My english dictionary isn't quite developed, so should you have any recommendations with how I should describe things. Please be specific, and concrete.

@foreach (var parentFolder in Model)
{
Stack<Folder> folderStack = new Stack<Folder>();
folderStack.Push(parentFolder);
var currentFolder = folderStack.Pop();
int dummyCounter = 1;

//Parent folder
<div class="row">
    <div class="col-sm-2">
        <a class="btn"
           role="button"
           data-toggle="collapse"
           href="#@currentFolder.Id"
           aria-expanded="false"
           aria-controls="@currentFolder.Id">
            <span class="@GlyphionCategoryIcon"></span>
        </a>
    </div>
    <div class="col-sm-5">@currentFolder.Id</div>
    <div class="col-sm-5">@currentFolder.Name</div>
</div>

<div class="collapse" id="@currentFolder.Id">

    @if (currentFolder.FoldersContained != 0)
    {
        do
        {
            //Prevents a copy of the parent folder, otherwise this display nested folders
            if (dummyCounter != 1)
            {
                <div class="row">
                    <div class="col-sm-2">
                        <a class="btn"
                           role="button"
                           data-toggle="collapse"
                           href="#@currentFolder.Id"
                           aria-expanded="false"
                           aria-controls="@currentFolder.Id">
                            <span class="@GlyphionCategoryIcon"></span>
                        </a>
                    </div>
                    <div class="col-sm-5">@currentFolder.Id</div>
                    <div class="col-sm-5">@currentFolder.Name</div>
                </div>
            }
            // Create a collapse div using bootstrap 3.3.7
            <div class="collapse" id="@currentFolder.Id">
                @if (currentFolder.FoldersContained > 0)
                {
                    for (int i = currentFolder.FoldersContained; i > 0; i--)
                    {
                        //Pushes all nested directories into my stack
                        //in reverse inorder to display the top directory
                        folderStack.Push(currentFolder.Folders[i - 1]);
                        dummyCounter++;
                    }
                }

                @if (currentFolder.FilesContained != 0)
                {
                    // Should they contain any files, display them
                    foreach (var file in currentFolder.Files)
                    {
                        <div class="row">
                            <div class="col-sm-2">
                                <a class="btn"
                                   role="button"
                                   href="@webUrl@file.Url"
                                   target="_blank">
                                    <span class="@GlyphionPaperIcon"></span>
                                </a>
                            </div>
                            <div class="col-sm-5">@file.Id</div>
                            <div class="col-sm-5">@file.Name</div>
                        </div>
                    }
                }
            </div>

            //Ends the while loop
            if (folderStack.Count == 0)
            {
                dummyCounter = 0;
            }

            //Prepares the next nested folder object
            if (folderStack.Count != 0)
            {
                currentFolder = folderStack.Pop();
            }

        // I make use of a dummy counter inorder to break the loop
        // should there no longer be any nested directories and files
        // left to display
        } while (dummyCounter != 0);
    }

    //Finally, display all files in the parent folder, should there be any
    @if (parentFolder.FilesContained != 0)
    {
        foreach (var file in parentFolder.Files)
        {
            <div class="row">
                <div class="col-sm-2">
                    <a class="btn"
                       role="button"
                       href="@webUrl@parentFolder.Url"
                       target="_blank">
                        <span class="@GlyphionPaperIcon"></span>
                    </a>
                </div>
                <div class="col-sm-5">@parentFolder.Id</div>
                <div class="col-sm-5">@parentFolder.Name</div>
            </div>

        }
    }
</div>
}

Output: (dummy data, folders expanded) enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ The site standard is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles. \$\endgroup\$ – BCdotWEB Dec 17 '18 at 12:59
  • \$\begingroup\$ @BCdotWEB Could you give an example? My code simply takes an object<Folder> and iterates through its content and display it in html. I'm using asp.net-mcv and the code is writing in razor. \$\endgroup\$ – Alexander Lawaetz Dec 17 '18 at 13:40
  • 1
    \$\begingroup\$ For one, no need for "ASP.NET MVC Razor", that's what the tags are for. But "dynamic folder iteration HTML display" is less of a sentence, and more a bunch of words. "Displays it in HTML" is needlessly technical. Why not something like "Navigating the contents of folders on a website"? However, I don't know what your code is supposed to do, so that's something you need to describe. \$\endgroup\$ – BCdotWEB Dec 17 '18 at 15:04
  • 2
    \$\begingroup\$ This is what I call a fix! Great job ;-) \$\endgroup\$ – t3chb0t Dec 17 '18 at 17:46
  • \$\begingroup\$ @t3chb0t Thanks for your kind message, greatly appriciated :) \$\endgroup\$ – Alexander Lawaetz Dec 17 '18 at 21:11
2
\$\begingroup\$

I have just two comments:

  • The name dummyCounter is really terrible, you should find something more appropriate like currentDepth or something but you actually don't need this at all, you can use the folderStack and just ask it whether it's not empty with folderStack.Any()
  • You use the same html snippet four times (!)

    <div class="row">
        <div class="col-sm-2">
            <a class="btn"
            role="button"
            data-toggle="collapse"
            href="#@currentFolder.Id"
            aria-expanded="false"
            aria-controls="@currentFolder.Id">
                <span class="@GlyphionCategoryIcon"></span>
            </a>
        </div>
        <div class="col-sm-5">@currentFolder.Id</div>
        <div class="col-sm-5">@currentFolder.Name</div>
    </div>
    

    This should be a partial view that you can reuse instead of copy-pasting it everywhere. The values that are chaniging can be passed via its own new model.

\$\endgroup\$
  • \$\begingroup\$ Thanks for your insight, I will differently look into how partial view works and it's implementation. However considering your first suggestment, I have already tired using my stack as a way to end my while loop. But it lead to missing out on the last folder, as the while loop would end after I pop the last element to prepare my the "final" loop. I also haven't firgured out a way to print my parent folder without using an if statement to skip my second folder "creating" in order to prevent duplicating my parent folder. \$\endgroup\$ – Alexander Lawaetz Dec 19 '18 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.