Hi,

Searching through the message board I came across this code posted by HeSaidJoe...

Code:
'as for the directories.


'however it is better than just reading the drive as it sends the names
'all over hell's half acre.

'sub to sort the array holing the directroy names
'also, this does not include sub directorys under these directories
Option Explicit
Option Compare Text

Sub SortArr(iArray As Variant)
     
     Dim lLoop1 As Long
     Dim lLoop2 As Long
     Dim lTemp As String
          
  
     For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
       For lLoop2 = LBound(iArray) + 1 To lLoop1

         If iArray(lLoop2 - 1) > iArray(lLoop2) Then
           lTemp = iArray(lLoop2 - 1)
           iArray(lLoop2 - 1) = iArray(lLoop2)
           iArray(lLoop2) = lTemp
         End If
       Next lLoop2
     Next lLoop1
   End Sub

'use the FileSystemObject to access the folders

Sub ShowFolderList(folderspec)
    Dim myArr()
    Dim fs, f, f1, s, sf, i
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set sf = f.subfolders
    
    For Each f1 In sf
        s = f1.Name
        
'load the names into an array
        ReDim Preserve myArr(i)
        myArr(i) = (folderspec & s)
        i = i + 1
        s = s & vbCrLf
    Next
    
 'sort the array
   Call SortArr(myArr)
   
'open a text file and store the names in it

  Dim myFile As String, intNum As Integer
  myFile = "C:\myFile.txt"
  intNum = FreeFile

'this will add the dir to the end of the 
'file containing the file names
  
  Open myFile For Append As FreeFile
      For i = LBound(myArr) To UBound(myArr)
          Print #intNum, myArr(i)
      Next i
        Close intNum
         
End Sub
Just what I was after, however when I run it it throws up a Run-Time Error '9' - Subscript out of range.

It happens at the following line...

Code:
Sub SortArr(iArray As Variant)
     
     Dim lLoop1 As Long
     Dim lLoop2 As Long
     Dim lTemp As String
          
  
     For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
       For lLoop2 = LBound(iArray) + 1 To lLoop1

         If iArray(lLoop2 - 1) > iArray(lLoop2) Then
           lTemp = iArray(lLoop2 - 1)
           iArray(lLoop2 - 1) = iArray(lLoop2)
           iArray(lLoop2) = lTemp
         End If
       Next lLoop2
     Next lLoop1
     
End Sub
Any ideas why?

Regards.