# 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 contains nested sub-directories, list their files as well.

Background: It was bought to my attention that the use of dummyCounter is considered bad practices. But, I still haven't managed to find a different approach that suits my needs. Currently, my dummyCounter is used to prevent duplicating the parentFolder, as well as allowing me to break my loop.

My previous approach: I have used folderStack.Any() as a condition for my while loop, but that resulted in the last nested sub-directory not getting shown. Then I tried using a "dept counter" but that proved too difficult to intercorporate while using a Stack approach to hold my data.

I would greatly appreciate community feedback. And should there be any problems with my post regarding community rules, let me know. - I tried not to repeat any of my previous issues.

_Folder Partial View

@{
string GlyphionFolderIcon = "glyphicon glyphicon-folder-open";
}
<div class="row">
<div class="col-sm-2">
<a class="btn"
role="button"
data-toggle="collapse"
href="#@Model.Id"
aria-expanded="false"
aria-controls="@Model.Id">
<span class="@GlyphionFolderIcon"></span>
</a>
</div>
<div class="col-sm-5">@Model.Id</div>
<div class="col-sm-5">@Model.Name</div>


_File Partial View

@{
string GlyphionModelIcon = "glyphicon glyphicon-paperclip";
}

<div class="row">
<div class="col-sm-2">
<a class="btn"
role="button"
href="@webUrl@Model.Url"
target="_blank">
<span class="@GlyphionModelIcon"></span>
</a>
</div>
<div class="col-sm-5">@Model.Id</div>
<div class="col-sm-5">@Model.Name</div>
</div>


_Layout View

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

//Parent folder
@Html.Partial("_Folder", parentFolder);

<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)
{
@Html.Partial("_Folder", currentFolder);
}

<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)
{
@Html.Partial("_File", file);
}
}
</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)
{
@Html.Partial("_File", file);
}
}
</div>
}

• Oh yeah, this is much cooler now ;-) – t3chb0t Dec 20 '18 at 12:48
• @t3chb0t Thank you. I'm glad for the continuous support you provided so far. It has given me a good impression of code review while introducing me to some of its guidelines =) – Alexander Lawaetz Dec 20 '18 at 13:04

You should remove

int dummyCounter = 1;


and change the if's checking for folderStack.Count to an if..else like so

        //Ends the while loop
if (folderStack.Count == 0)
{
break;
}
else
{
currentFolder = folderStack.Pop();
}


if (dummyCounter != 1)
{
@Html.Partial("_Folder", currentFolder);
}


as well and again checked the program-flow. If we assume that the parentFolder.FoldersContained != 0 we output the files of the parentFolder twice. One time inside the do..while loop and one time after the loop. We could remove this if and this

//Parent folder
@Html.Partial("_Folder", parentFolder);


like so

@foreach (var parentFolder in Model)
{

Stack<Folder> folderStack = new Stack<Folder>();
folderStack.Push(parentFolder);

@while (folderStack.Count > 0)
{

var currentFolder = folderStack.Pop();
@Html.Partial("_Folder", currentFolder);

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

@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]);
}

// Should they contain any files, display them
@foreach (var file in currentFolder.Files)
{
@Html.Partial("_File", file);
}

</div>

}
}


I know that this isn't exactly the same as you had before (the most outer <div class="collapse" id="@currentFolder.Id"> is missing) but it is much clearer and easier to read. If you need this div's I would suggest to just use a separate method to process the contained folders and files of the parentFolders.

• Thanks for the advices, even if they didn't nessercary solve the issue. I will how ever use them for further improvements. I think you're right about finding a different approach. And also made me aware that, just like your code doesn't work with having subdirectories showned under my parent folder. Any subdirectories greater than 1 level down wouldn't be showned as expected either unfortunately. Wasn't aware of that mistakes giving the data I was working with didn't offer such a senario. Much appriciated. – Alexander Lawaetz Jan 16 at 19:15