Solved

how do I take the array content of GetFiles and GetDirectories to creat an alphabetical listing of first characters removing all duplicated?

Posted on 2008-06-15
13
460 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:andyk0726
  • 6
  • 6
13 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 21788710
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
 

Author Comment

by:andyk0726
ID: 21788810
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
 
LVL 1

Accepted Solution

by:
trenduin earned 500 total points
ID: 21789582
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
 
LVL 1

Expert Comment

by:trenduin
ID: 21789619
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
 

Author Comment

by:andyk0726
ID: 21789815
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
 

Author Comment

by:andyk0726
ID: 21790070
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:trenduin
ID: 21791364
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
 
LVL 1

Expert Comment

by:trenduin
ID: 21791373
Oh wait you meant in the letters. LOL my bad.
0
 
LVL 1

Expert Comment

by:trenduin
ID: 21791376
You could also check to see if the letter is a number and then just add the # sign instead of the numbers.
0
 

Author Comment

by:andyk0726
ID: 21791835
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
 

Author Comment

by:andyk0726
ID: 21795888
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
 

Author Closing Comment

by:andyk0726
ID: 31467356
need to view further posts to fully use this code
0
 
LVL 1

Expert Comment

by:trenduin
ID: 21798190
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Pass through dll 2 36
display data in text field from data base for updating 6 26
ConsoleSql 1 14
XML & .net 5 16
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now