andyk0726
asked on
how do I take the array content of GetFiles and GetDirectories to creat an alphabetical listing of first characters removing all duplicated?
Hi,
I'm really new to c#, so please take it easy on me.
I wish to be able to use the methods GetFiles and GetDirectories to return a listing of all of the files and directories of a virtual directory, keepthing the paths relative to the root of the webserver, not the file system location.
Then from the two array listings, take only the first character from each line, of each array for the name of the item, bringing the list down to 1st characters only, then combine the two arrays, to create 1 array, maybe first maybe last taking the time to remove all duplicates.
Once I have the list I'm gona use an asp repeater to set it up as a <ul>, but that's the easy part.
Thanks in advance
Andy
I'm really new to c#, so please take it easy on me.
I wish to be able to use the methods GetFiles and GetDirectories to return a listing of all of the files and directories of a virtual directory, keepthing the paths relative to the root of the webserver, not the file system location.
Then from the two array listings, take only the first character from each line, of each array for the name of the item, bringing the list down to 1st characters only, then combine the two arrays, to create 1 array, maybe first maybe last taking the time to remove all duplicates.
Once I have the list I'm gona use an asp repeater to set it up as a <ul>, but that's the easy part.
Thanks in advance
Andy
Array.Sort(Your Array); is the method of alphabetic sorting. What i'm confused about is the single letter thing. if they begin with the same letter how are you going to distinguish different items?
ASKER
What I want to get to is a html output of
<ul id="alphabet_jump">
<li><a href="#a">a</a></li>
<li><a href="#b">b</a></li>
..
<li><a href="#z">z</a></li>
</ul>
Where the list is only populated from the result of the directory listing, including both files and directories.
Lower down the pages I will create the
<ol>
<li id="a">contents of file and directory listing starting with the letter A</li>
<li id="b">contents of file and directory listing starting with the letter B</li>
..
<li id="z">contents of file and directory listing starting with the letter Z</li>
</ol>
so if there are no files or directories begining with that letter/character if will not appear in the upper <ul>
<ul id="alphabet_jump">
<li><a href="#a">a</a></li>
<li><a href="#b">b</a></li>
..
<li><a href="#z">z</a></li>
</ul>
Where the list is only populated from the result of the directory listing, including both files and directories.
Lower down the pages I will create the
<ol>
<li id="a">contents of file and directory listing starting with the letter A</li>
<li id="b">contents of file and directory listing starting with the letter B</li>
..
<li id="z">contents of file and directory listing starting with the letter Z</li>
</ol>
so if there are no files or directories begining with that letter/character if will not appear in the upper <ul>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oh I forgot to mention that
//create a string that holds the current directory
string curDir = Directory.GetCurrentDirect ory().ToSt ring();
gets the path of the current file running the code. You should be able to change this to whatever you want. Just remove the Directory.GetCurrentDirect ory().ToSt ring(); and hard code the directory you want to use, or use different code to the the directory. Since I'm not sure what your directory structure is, I just used this for testing.
//create a string that holds the current directory
string curDir = Directory.GetCurrentDirect
gets the path of the current file running the code. You should be able to change this to whatever you want. Just remove the Directory.GetCurrentDirect
ASKER
tried it out briefly and got no errors once I removed the using linq; references. Seems to be the business.
Not haing linq installed on this machine.
I'll give it a good look tomorrow.
Many thanks
Not haing linq installed on this machine.
I'll give it a good look tomorrow.
Many thanks
ASKER
Ok so I looked a little longer.
I had to convert tmp to be uppercase so that the list items were not duplicated. but appart from that the codes cool.
many thanks for your help.
I had to convert tmp to be uppercase so that the list items were not duplicated. but appart from that the codes cool.
many thanks for your help.
using System;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
//added the following
using System.Collections;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//create an array list to hold files and folders
ArrayList myFilesFolders = new ArrayList();
//create a string that holds the current directory
string curDir = Directory.GetCurrentDirectory().ToString();
//get directory info and files in current directory
string[] dirListing = Directory.GetDirectories(curDir);
string[] fileListing = Directory.GetFiles(curDir);
//add directoris to the list
for (int i = 0; i < dirListing.Length; i++)
{
string tmp = dirListing[i];
tmp = tmp.Replace(curDir, "");
tmp = tmp.Replace("\\", "");
myFilesFolders.Add(tmp);
}
//add files to the list
for (int i = 0; i < fileListing.Length; i++)
{
string tmp = fileListing[i];
tmp = tmp.Replace(curDir, "");
tmp = tmp.Replace("\\", "");
myFilesFolders.Add(tmp);
}
//sort list
myFilesFolders.Sort();
//get letters in different array list
ArrayList myLetters = new ArrayList();
//now we want to add the first letter of each item only once
for (int i = 0; i < myFilesFolders.Count; i++)
{
//get the first character of the current item, convert to upper case to stop duplicates later in sort.
char[] chrArray = myFilesFolders[i].ToString().ToCharArray(0, 1);
string tmp = chrArray[0].ToString().ToUpper();
//now see if that item is in the current my letters list
if (!myLetters.Contains(tmp))
//add it to the list
myLetters.Add(tmp);
}
//now sort the list of letters
//should be already sorted but we'll just make sure
myLetters.Sort();
//now we can add these items to a regular array if an array list won't work
string[] ffList = (string[]) myFilesFolders.ToArray(typeof(string));
string[] letterList = (string[]) myLetters.ToArray(typeof(string));
//clean up
myLetters.Clear();
myFilesFolders.Clear();
}
}
How could you possibly have duplicates? It's kind of hard to have two files with the exact same name in the exact same folder.
Oh wait you meant in the letters. LOL my bad.
You could also check to see if the letter is a number and then just add the # sign instead of the numbers.
ASKER
I am thinking about having that, all numbers together, all weird characters together ($ [ ] ) etc... and then split up the letters into their correct groups. When I do decided I will probably be back for help.
And yes it was just the upper and lower case versions of the same letters that were causing me an issue, i.e. a A, would give two results when I only wanted 1, and chose uppercase as it looks nicer.
Thanks for the suggestions
And yes it was just the upper and lower case versions of the same letters that were causing me an issue, i.e. a A, would give two results when I only wanted 1, and chose uppercase as it looks nicer.
Thanks for the suggestions
ASKER
here is the code that I used the get the virtual directory listing.
If I used your original I got the windows\system directory, and various others put me in the .Net framework sub-directories. this comes back with the goods.
Apparently MapPath will give you the relative path when you do not start with a \ or a /. If you start with either of those it will go from the root of the files system.
If I used your original I got the windows\system directory, and various others put me in the .Net framework sub-directories. this comes back with the goods.
Apparently MapPath will give you the relative path when you do not start with a \ or a /. If you start with either of those it will go from the root of the files system.
string curDir = Server.MapPath(@"./");
ASKER
need to view further posts to fully use this code
Yup. The application directory is the location of the .Net compiler, since it is what renders the pages. I just used it to test because there were files in there and I didn't wan to mess around with uploading to a server since I don't have .Net setup on my servers yet. All still in ASP with VB Script.