HI, it's me again. I have a directory named photouploads which holds all the photos uploaded to the website. I also used the jQuery UI accordion to hold the list of files inside the photouploads helper. If I am going to sort the files alphabetically and
use alphabets to be the header for the accordion, how can I put the files into the header in which their filenames belong?
Here is my code for the file list:
@{
var folder = "photouploads";
var path = Path.Combine(Server.MapPath("~/"), folder);
}
<ul>
@foreach(var item in Directory.GetFiles(path)){
<li>@Path.GetFileName(item)</li>
}
</ul>
Instead of using the Directory object, try a DirectoryInfo object.
DirectoryInfo dir = new DirectoryInfo(path);
Then you can use the dir.GetFiles("b*",false); to return an array of FileInfo[] objects. The GetFiles in DirectoryInfo is different than Directory because it takes a search parameter instead of a path. The second parameter tells it whether or not to search
sub-directories. I'd write out the MVC, but I'm not profient at it yet but you shouldn't have any trouble. For more info on using the DirectoryInfo.GetFiles method see
http://msdn.microsoft.com/en-us/library/ms143327.aspx
AccordionJQuery
Don't forget to mark useful responses as Answer if they helped you towards a solution.
Thank you for the link. I am looking for a Web Pages way to do it. I guess the code snippets in the msdn can help me to create the solution I want to have.
For update: I used the Directory.GetFiles, added another parameter for the search pattern and voila! it worked. But I already tried this into a single alphabet, if I am going to use a loop to get all the letters, how ? Here is the current code:
@{
var folder = "photouploads";
var path = Path.Combine(Server.MapPath("~/"), folder);
var dir = Directory.GetFiles(path, "a*");
}
<ul>
@foreach(var item in dir){
<li>@Path.GetFileName(item)</li>
}
</ul>
I have a partial solution for you. This is the only thing I've formulated so far, I am also a beginner. I hope the experts from this forum can help you to improve the solution I have. The part I can't solve is the second parameter for Directory.GetFiles
method (commented below).
@{
var folder = "photouploads";
var path = Path.Combine(Server.MapPath("~/"), folder);
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
<script>
$(function() {
$( "#fileList" ).accordion();
});
</script>
<div id="fileList">
@foreach(char l in alphabet){
<h3>@l</h3>
<div>
<ul>
@foreach(var item in Directory.GetFiles(path, "f*")){
@* The second parameter for the method above must be dynamic, based on current loop *@
<li>@Path.GetFileName(item)</li>
}
</ul>
</div>
}
</div>
thunderbark
Member
12 Points
9 Posts
Sort files inside a directory in alphabetical order
Mar 02, 2012 03:30 AM|LINK
HI, it's me again. I have a directory named photouploads which holds all the photos uploaded to the website. I also used the jQuery UI accordion to hold the list of files inside the photouploads helper. If I am going to sort the files alphabetically and use alphabets to be the header for the accordion, how can I put the files into the header in which their filenames belong?
Here is my code for the file list:
@{ var folder = "photouploads"; var path = Path.Combine(Server.MapPath("~/"), folder); } <ul> @foreach(var item in Directory.GetFiles(path)){ <li>@Path.GetFileName(item)</li> } </ul>The accordion that I want to accomplish:
Accordion JQuery
markfitzme
All-Star
15347 Points
2360 Posts
Re: Sort files inside a directory in alphabetical order
Mar 02, 2012 04:40 AM|LINK
Instead of using the Directory object, try a DirectoryInfo object.
DirectoryInfo dir = new DirectoryInfo(path);
Then you can use the dir.GetFiles("b*",false); to return an array of FileInfo[] objects. The GetFiles in DirectoryInfo is different than Directory because it takes a search parameter instead of a path. The second parameter tells it whether or not to search sub-directories. I'd write out the MVC, but I'm not profient at it yet but you shouldn't have any trouble. For more info on using the DirectoryInfo.GetFiles method see http://msdn.microsoft.com/en-us/library/ms143327.aspx
Accordion JQuery
thunderbark
Member
12 Points
9 Posts
Re: Sort files inside a directory in alphabetical order
Mar 02, 2012 08:19 PM|LINK
Thank you for the link. I am looking for a Web Pages way to do it. I guess the code snippets in the msdn can help me to create the solution I want to have.
Accordion JQuery
thunderbark
Member
12 Points
9 Posts
Re: Sort files inside a directory in alphabetical order
Mar 02, 2012 08:41 PM|LINK
For update: I used the Directory.GetFiles, added another parameter for the search pattern and voila! it worked. But I already tried this into a single alphabet, if I am going to use a loop to get all the letters, how ? Here is the current code:
@{ var folder = "photouploads"; var path = Path.Combine(Server.MapPath("~/"), folder); var dir = Directory.GetFiles(path, "a*"); } <ul> @foreach(var item in dir){ <li>@Path.GetFileName(item)</li> } </ul>Thank you!
freakysquash
Member
228 Points
155 Posts
Re: Sort files inside a directory in alphabetical order
Mar 02, 2012 09:26 PM|LINK
I have a partial solution for you. This is the only thing I've formulated so far, I am also a beginner. I hope the experts from this forum can help you to improve the solution I have. The part I can't solve is the second parameter for Directory.GetFiles method (commented below).
@{ var folder = "photouploads"; var path = Path.Combine(Server.MapPath("~/"), folder); const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } <script> $(function() { $( "#fileList" ).accordion(); }); </script> <div id="fileList"> @foreach(char l in alphabet){ <h3>@l</h3> <div> <ul> @foreach(var item in Directory.GetFiles(path, "f*")){ @* The second parameter for the method above must be dynamic, based on current loop *@ <li>@Path.GetFileName(item)</li> } </ul> </div> } </div>Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: Sort files inside a directory in alphabetical order
Mar 02, 2012 10:07 PM|LINK
Try this:
@{ var folder = "photouploads"; var path = Path.Combine(Server.MapPath("~/"), folder); var files = Directory.GetFiles(path).Select(f => Path.GetFileName(f)).OrderBy(f => f); var fileGroups = files.GroupBy(f => Char.ToUpper(f[0])).Select(g => new { FirstLetter = Char.ToUpper(g.Key), Files = g }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> @foreach(var fileGroup in fileGroups){ <h3>@fileGroup.FirstLetter</h3> <ul> @foreach (var file in fileGroup.Files){ <li>@file</li> } </ul> } </body> </html>Web Pages CMS | My Site | Twitter
thunderbark
Member
12 Points
9 Posts
Re: Sort files inside a directory in alphabetical order
Mar 03, 2012 12:12 AM|LINK
Thank you Mikesdotnetting! that's an awesome solution.