Results 1 to 25 of 25

Thread: [RESOLVED] List Directory and Arrange by type

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Resolved [RESOLVED] List Directory and Arrange by type

    Hi,

    Is it possible to achieve that by using Directory class?

    I tried this method by http://www.java2s.com/Code/VB/File-D...riesinroot.htm and it seems to list my folders by date.

    Please advise.

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: List Directory and Arrange by type

    If you mean get the all the file names from a folder and then sort by the extension then yes, it's possible. You might want to follow the blog link in my signature about sorting arrays and collections. You can call Directory.GetFiles or DirectoryInfo.GetFiles to get an array of Strings or FileInfos respectively and then use a Comparison or IComparer to sort by file extension. For strings you can use Path.GetExtension to get each file extension and for FileInfos you can use their Extension property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    This is the way I would do it, which is pretty much what JMC said but just thought I would provide a working example to help. This is just a console application but obviously you could modify it to work with any .NET app very easily.

    vb Code:
    1. 'This is the function that compares the extensions using
    2.     'String.Compare on the file extensions
    3.     Private Function ExtensionCompare(ByVal x As String, ByVal y As String) As Integer
    4.         Return String.Compare(IO.Path.GetExtension(x), IO.Path.GetExtension(y))
    5.     End Function
    6.  
    7.     Sub Main()
    8.  
    9.         'Get the list of files in the C drive root
    10.         Dim FileList As List(Of String) = IO.Directory.GetFiles("C:\").ToList
    11.  
    12.         'Sort the files using the ExtensionCompare function
    13.         'which is defined above
    14.         FileList.Sort(New Comparison(Of String)(AddressOf ExtensionCompare))
    15.  
    16.         'Loop through the list from start to end and write each
    17.         'value to the screen
    18.         For i As Integer = 0 To FileList.Count - 1
    19.             Console.WriteLine(FileList(i))
    20.         Next
    21.  
    22.         'Pause
    23.         Console.ReadLine()
    24.  
    25.     End Sub
    Last edited by chris128; Jul 13th, 2009 at 01:20 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    Thanks both for replying I tried the 2nd coding and this line seems to have error:
    Dim FileList As List(Of String) = IO.Directory.GetFiles("C:\").ToList
    As It doesnt seems to have ToList method.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    It does have that method as I compiled and ran that code before I posted it. You must have changed something from the code that I posted - either that or perhaps you are using a different version of the framework (but you marked this thread as 2008 so I assume not).

    Can you post your code?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    I tried console application and it works without error, for my win32 app, it shows 'ToList' is not a member of system.array

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    What do you mean by Win32 app? It works fine in a windows forms app (VB.NET) when I just tested it. You're not using C++ are you?
    Last edited by chris128; Jul 13th, 2009 at 01:21 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    Ah yeah that method doesnt exist for .NET 2.0 apps but as the OP marked this as a VS 2008 thread and didnt indiciate he wasnt using 3 / 3.5 I foolishly assumed he was using that lol
    Thats one thing I have often wondered - wouldnt it be better for threads to be marked with their .NET framework version rather than the version of VS used? As the framework version has a much bigger bearing on what can be done than VS does (I guess this wasnt a problem until 2008 came out as that introduced the ability to target specific versions of the framework)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: List Directory and Arrange by type

    Quote Originally Posted by chris128 View Post
    ... wouldnt it be better for threads to be marked with their .NET framework version rather than the version of VS used?
    Agreed!

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    Quote Originally Posted by ForumAccount View Post
    Agreed!
    http://www.vbforums.com/showthread.php?p=3558729
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    I am using .NET Framework 3.5 SP1. The weird part in my console app no problem but not my win32. Maybe I try recreate my project again.

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    What do you mean by win32? Is this a VB.NET project or a C++ project?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: List Directory and Arrange by type

    Are you sure your WinForms app (not Win32, which just means 32-bit Windows) is not targeting .NET 2.0 or 3.0? Also, check that you haven't removed the required reference or import from your project. The assembly is System.Core.dll and the namespace is System.Linq.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    I tried create a new blank win32 and it works. Since you all were talking about the .NET Framework, I suspect that the project that I'm working on is running the old .NET Framework?

    As I think I have already do the conversion, but can I compile it as 3.5 or add some reference to make sure the .ToList is recognize?

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    Its a Windows Form VB.NET project. I can't import System.LINQ, I think the problem is really related to .NET Framework. As I try to add a reference of System.Core, its greyed out and also other 3.5 references.

    Is there a way to convert to 3.5 from 2.0? I remember I did went through the conversion process.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: List Directory and Arrange by type

    It's obviously not targeting .NET 3.5 at the moment so you must have chosen 2.0 when you created the project. You can change the target version from the Advanced Compile Options in the project properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    Just open up your project properties (double click My Project in Solution Explorer) and go to the Compile section, click Advanced and then change the target type from .NET 2 to 3.5

    EDIT: Looks like JMC beat me to it
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    Hi all, thanks it works. You 2 have been a great help. Btw, how should I sort it by alphabetically order because the directory I am testing inside have the same extension.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: List Directory and Arrange by type

    I believe that my blog post covers sorting by two properties. You can use the Path class to get the extension and the file name, or the FileInfo class does the same.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    The code I posted in Post #3 does exactly that doesnt it?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    I list my C:\ and that's the output
    bootmgr
    autoexec.bat
    pagefile.sys
    hiberfile.sys
    config.sys

    I think is arrange by type, I want to arrange by ascending order as well.

  23. #23
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: List Directory and Arrange by type

    Ahhh I see, sorry I thought you just meant the type needed to be in alphabetical order (as you did not specify).
    I'm sure you could modify the code I posted to incorporate such functionality but I am not quite sure how and I'm afraid I havent got time at the moment to have a play around
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    Yup, coz at first I thought that will be some built in method to arrange by type/date/ etc. Didn't know that need to code extra method, I think I would take a look at the sorting part. But nevertheless, thanks alot for the help

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: List Directory and Arrange by type

    while reading jmcilhinney blog, I found out that actually the .Sort method helps me sort in ascending order for alphanumeric. That's good. Thanks alot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width