Results 1 to 6 of 6

Thread: a directory list of files to a file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    Oshawa, Ontario, Canada
    Posts
    88
    i was wondering if anyone could help me out with one little problem

    how can i put a complete list of files from a specified directory using vb?? i want the list to look like this...

    file1.zip
    file2.zip
    file3.zip
    file4.zip
    file5.zip
    file6.zip
    etc....
    all in one text file

    please help me out with this!
    -|Kn|gHt|
    MSN - [email protected]

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    'access all files within a folder
    
        Dim stFile As String
        Dim stDir As String
        Dim i As Integer, intNum As Integer
        Dim myArr()
        intNum = FreeFile
    
    'use the folder my documents and list only zip files
        stDir = "C:\my Documents\"
        stFile = Dir$(stDir & "*.zip")
    
    Do While stFile <> ""
    'if you want to access each file you must use the dir
    'and the file
    
    ReDim Preserve myArr(i)
      myArr(i) = stFile
      i = i + 1
      stFile = Dir
    Loop
    'open the file for output and add the filenames
     Open "C:\myfile.txt" For Output As intNum
       For i = LBound(myArr) To UBound(myArr)
          Print #intNum, myArr(i)
         
       Next i
     Close intNum
     
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Lively Member
    Join Date
    Apr 2000
    Posts
    70
    how about:
    Code:
    x = Shell("dir c:\path\to\directory\*zip > file.txt",vbhidden)
    you'd get lots of file sizes etc though

    [Edited by CAPHS on 11-05-2000 at 05:06 AM]
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

  4. #4
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    caphs was on the right track. Note the /b
    Code:
    Shell "dir c:\path\to\directory\*zip /b > file.txt",vbhidden

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    Oshawa, Ontario, Canada
    Posts
    88
    the code from the first reply worked perfectly....those shell commands don't work properly worth crap...

    does anyone know how to also put a directory list from
    c:\ to a textfile as well as single files in the c:\

    so i get a list like

    dir1
    dir2
    dir3
    dir4
    file1
    file2
    file3
    file4

    etc.....all in one text file.....???
    -|Kn|gHt|
    MSN - [email protected]

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'For the single files use the code I gave you above. Just change these
    'two lines.
    
    'use the folder my documents and list only zip files
        stDir = "C:\my Documents\"
        stfile = Dir$(stDir & "*.zip")
    'Change them to this
        stDir = "C:\"
        stfile = Dir$(stDir & "*.*")
     '[/b]
    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

    Private Sub Form_Load()
    Call ShowFolderList("C:\")

    End Sub


    [Edited by HeSaidJoe on 11-05-2000 at 07:35 PM]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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