Results 1 to 5 of 5

Thread: Dicertory list to File

  1. #1

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    Question Dicertory list to File

    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.
    VB.Net (VS 2010)

  2. #2
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    put a breakpoint on that line, and look at the values of ubound(whatever) ( and lbound).
    i imagine theres some prob with the array, maybe you`ve picked an empty directory or something?

  3. #3

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507
    I've had a look through it, and the problem appears to be with the 'Scripting.FileSystemObject'.

    I put a break point in at...

    Code:
        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
    It skips the line highlighted bold?

    Regards
    VB.Net (VS 2010)

  4. #4
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    sf and/or fs may be wrong.

    you are defining them as a Variant, maybe try making it an object:

    dim sf as object

    i`ve not used this object before. are there any simple examples of using it anywhere? (msdn, internet)?

  5. #5
    Hyperactive Member Simon Caiger's Avatar
    Join Date
    Aug 2000
    Location
    Rugby, England
    Posts
    377
    I had the same problem using the file system object.
    Worked fine in the VB environment but fell over when running the .exe.
    Problem resolved when I remembered to include "Microsoft Scripting Runtime" in the project references.
    I realize this thread is a bit old and the original posters will have fixed the problem by now, but maybe it will help someone else searching for a solution to the same problem.
    Simon Caiger

    Documentation is like sex; when it's good, it's very, very good, and when it's bad, it's better than nothing.

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