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>
}