Results 1 to 10 of 10

Thread: read files in folder and asign file names into an array

  1. #1

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    read files in folder and asign file names into an array

    how would i read inside a directory, all the files. i want to take the names of the files and assign them to a string array.

    also the user is going to be choosing a folder. so how can they choose the folder they want? like i need almost like a file save as thing, where you choose your location, but they are picking a folder.

    -JOe
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: read files in folder and asign file names into an array

    Joe, the easiest way to get the files/folders within a selected folder:
    Code:
    Dim sFile As String
    sFile = Dir$([selectedFolderPath] & "\*.*",  vbDirectory Or vbHidden Or vbSystem Or vbReadOnly or vbArchive)
    Do Until sFile = vbNullString
        If sFile <> "." Then ' relative path meaning this directory
           If sFile <> ".." Then ' relative path meaning parent directory
              ' add the file to your array here
           End If
        End If
        sFile = Dir$()
    Loop
    Recommend doing a quick search for "Browse for Folder". The code will be in a module or class, since VB has no control to do that functionality. The window displayed is folder based only, no files (though it can be set up that way), and something I'm sure you have seen before.
    Last edited by LaVolpe; Nov 17th, 2008 at 03:26 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: read files in folder and asign file names into an array

    how can the user choose the folder? oh... i just looked maybe that was the last part of your post...

    i need to get folder names inside that main folder and name them as strings too... how to do that?

    then i need to open those folders and name those files in that folder to strings, and thats all i need.. any other help?
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: read files in folder and asign file names into an array

    Quote Originally Posted by damasterjo
    how can the user choose the folder?
    As I suggested:
    Quote Originally Posted by LaVolpe
    Recommend doing a quick search for "Browse for Folder". The code will be in a module or class, since VB has no control to do that functionality. The window displayed is folder based only, no files (though it can be set up that way), and something I'm sure you have seen before.
    Once you have the folder to start in, are you saying you need to do a recursive search through all subfolders, all their subfolders, on an on?

    Edited: Do you need file names or folder names? If both, then the code I posted above will work. If just file names or just folder names, it needs to be tweaked a little bit.
    Last edited by LaVolpe; Nov 17th, 2008 at 03:17 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: read files in folder and asign file names into an array

    *main folder*
    file
    file
    file
    up to 5 *subfolders* in main folder
    file
    file
    file

    i need all files main folder and subfolders to be strings...
    is there anyway you could break down that code just a little more? right now i dont have vb6 i cant get it till tomorrow, i need to have this ready by then sorta

    EDIT: so basically im sure i can figure your code out when i can compile it and display the results and see what i see, but if you can explain alittle more i would really really apreciate it..

    EDIT 2: and i dont understand the "." and ".."
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: read files in folder and asign file names into an array

    Yes the code in post#2, should give you everything but the main folder. The main folder is the "selectedFolderPath" in that code, so you will already know its name.

    Keep this in mind, the results will be just the names, not the full path. The full path will be the main folder's path + the file/subfolder names
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: read files in folder and asign file names into an array

    what order are these files coming in?
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  8. #8

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: read files in folder and asign file names into an array

    why are there 2 if statements?
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: read files in folder and asign file names into an array

    Whatever order VB gives them to you, usually alphabetical.
    To determine if a name is a folder or file, the code modified a little...
    Code:
        Dim sFile As String
        Dim sDir As String
        sDir = "C:\Program Files\" ' make sure backslash is included
        sFile = Dir$(sDir & "*.*", vbArchive Or vbHidden Or vbReadOnly Or vbSystem Or vbDirectory)
        Do Until sFile = vbNullString
            If sFile <> "." Then ' relative path meaning this directory
                If sFile <> ".." Then ' relative path meaning parent directory
                    If (GetAttr(sDir & sFile) And vbDirectory) Then
                        Debug.Print "Folder: "; sFile
                    Else
                        Debug.Print "  File: "; sFile
                    End If
                End If
            End If
            sFile = Dir$()
        Loop
    Quote Originally Posted by damasterjo
    why are there 2 if statements?
    I explained in the comments within the code. Remove them and you will get folder names of ".." & "."
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: read files in folder and asign file names into an array

    ok thanks, looks like that will be great for now, i will be testing my uses tomorrow, if i need anymore info i will ask, thanks
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

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