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
andyk0726Asked:
Who is Participating?
 
trenduinConnect With a Mentor Commented:
Well, I think this is what you're wanting. I've tested it on my machine, but I didn't upload it to a web server. The code should work. I'm new to this as well, but I've recently been playing around a lot. =o)
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
            char[] chrArray = myFilesFolders[i].ToString().ToCharArray(0, 1);
            string tmp = chrArray[0].ToString();
 
            //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();
    }
}

Open in new window

0
 
silemoneCommented:
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?
0
 
andyk0726Author Commented:
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>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
trenduinCommented:
oh I forgot to mention that

//create a string that holds the current directory
string curDir = Directory.GetCurrentDirectory().ToString();

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.GetCurrentDirectory().ToString(); 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.
0
 
andyk0726Author Commented:
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
0
 
andyk0726Author Commented:
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.
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();
    }
}

Open in new window

0
 
trenduinCommented:
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.
0
 
trenduinCommented:
Oh wait you meant in the letters. LOL my bad.
0
 
trenduinCommented:
You could also check to see if the letter is a number and then just add the # sign instead of the numbers.
0
 
andyk0726Author Commented:
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
0
 
andyk0726Author Commented:
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.  
 string curDir = Server.MapPath(@"./");

Open in new window

0
 
andyk0726Author Commented:
need to view further posts to fully use this code
0
 
trenduinCommented:
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.
0
All Courses

From novice to tech pro — start learning today.