VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 306,728 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,530 people online right now. Registration is fast and FREE... Join Now!





Functions to Return Sorted Files and Folders

Functions that return sorted Files And Folders in FileInfo And DirectoryInfo Arrays.

Submitted By: dzone41
Actions:
Rating:
Views: 149

Language: VB.NET

Last Modified: November 4, 2009
Instructions: Imports System.IO
Requires an Path String and an integer. The integer would most likely come from selected menu sort options in User Interface.
The "If intSortBy" can be replaced with Select Case if more sort options are needed.

Snippet


  1.  
  2. Private Sub _GetFiles(ByVal strPath As String, ByVal intSortBy As Integer)
  3.     'Get all Directories in the Path
  4.     folders = New DirectoryInfo(strPath).GetDirectories
  5.     'These functions use the same array and returns it sorted
  6.     folders = SortFolders(folders, intSortBy)
  7.     'Get All Files in the Path
  8.     files = New DirectoryInfo(strPath).GetFiles()
  9.     files = SortFiles(files, intSortBy)
  10. End Sub
  11.  
  12. Private Function SortFolders(ByVal folders As DirectoryInfo(), ByVal intSortBy as Integer) As DirectoryInfo()
  13.     Dim intCount As Integer = folders.GetUpperBound(0)
  14.     'Must Delcare new array
  15.     Dim arrTempNames(intCount) As String
  16.     Dim intA As Integer = 0
  17.     'Iterate through folders
  18.     For Each dir As DirectoryInfo In folders
  19.         'Check sort option, this can be replaced with Select Case
  20.         If intSortBy = 0 Then
  21.             'assigns the chosen sortby element to a string array
  22.             arrTempNames(intA) = dir.Name
  23.         ElseIf intSortBy = 1 Then
  24.             arrTempNames(intA) = dir.LastWriteTime.ToString
  25.         End If
  26.     Next
  27.     Array.Sort(arrTempNames, folders)
  28.     Return folders
  29. End Function
  30.  
  31. Private Function SortFiles(ByVal files As FileInfo(), ByVal intSortBy As Integer) As FileInfo()
  32.     Dim intCount As Integer = files.GetUpperBound(0)
  33.     Dim arrTempNames(intCount) As String
  34.     Dim intA As Integer = 0
  35.     For Each dir As FileInfo In files
  36.         If intSortBy = 0 Then
  37.             arrTempNames(intA) = dir.Name
  38.         ElseIf intSortBy = 1 Then
  39.             arrTempNames(intA) = dir.LastWriteTime.ToString
  40.         End If
  41.     Next
  42.     Array.Sort(arrTempNames, files)
  43.     Return files
  44. End Function

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month