PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VB - List All The Files In A Directory-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 41

Thread: VB - List All The Files In A Directory

  1. #1

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    VB - List All The Files In A Directory

    This shows the filenames and not the total number of files in a directory.
    Attached Files Attached Files
    Last edited by manavo11; Sep 23rd, 2003 at 04:49 PM.


    Has someone helped you? Then you can Rate their helpful post.

  2. #2

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Another way (API) :

    VB Code:
    1. 'Create a form with a command button (command1), a list box (list1)
    2. 'and four text boxes (text1, text2, text3 and text4).
    3. 'Type in the first textbox a startingpath like c:\
    4. 'and in the second textbox you put a pattern like *.* or *.txt
    5.  
    6. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    7. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    8. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    9. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    10.  
    11. Const MAX_PATH = 260
    12. Const MAXDWORD = &HFFFF
    13. Const INVALID_HANDLE_VALUE = -1
    14. Const FILE_ATTRIBUTE_ARCHIVE = &H20
    15. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    16. Const FILE_ATTRIBUTE_HIDDEN = &H2
    17. Const FILE_ATTRIBUTE_NORMAL = &H80
    18. Const FILE_ATTRIBUTE_READONLY = &H1
    19. Const FILE_ATTRIBUTE_SYSTEM = &H4
    20. Const FILE_ATTRIBUTE_TEMPORARY = &H100
    21.  
    22. Private Type FILETIME
    23.     dwLowDateTime As Long
    24.     dwHighDateTime As Long
    25. End Type
    26.  
    27. Private Type WIN32_FIND_DATA
    28.     dwFileAttributes As Long
    29.     ftCreationTime As FILETIME
    30.     ftLastAccessTime As FILETIME
    31.     ftLastWriteTime As FILETIME
    32.     nFileSizeHigh As Long
    33.     nFileSizeLow As Long
    34.     dwReserved0 As Long
    35.     dwReserved1 As Long
    36.     cFileName As String * MAX_PATH
    37.     cAlternate As String * 14
    38. End Type
    39. Function StripNulls(OriginalStr As String) As String
    40.     If (InStr(OriginalStr, Chr(0)) > 0) Then
    41.         OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    42.     End If
    43.     StripNulls = OriginalStr
    44. End Function
    45.  
    46. Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
    47.     'KPD-Team 1999
    48.     'E-Mail: [email]KPDTeam@Allapi.net[/email]
    49.     'URL: [url]http://www.allapi.net/[/url]
    50.  
    51.     Dim FileName As String ' Walking filename variable...
    52.     Dim DirName As String ' SubDirectory Name
    53.     Dim dirNames() As String ' Buffer for directory name entries
    54.     Dim nDir As Integer ' Number of directories in this path
    55.     Dim i As Integer ' For-loop counter...
    56.     Dim hSearch As Long ' Search Handle
    57.     Dim WFD As WIN32_FIND_DATA
    58.     Dim Cont As Integer
    59.     If Right(path, 1) <> "\" Then path = path & "\"
    60.     ' Search for subdirectories.
    61.     nDir = 0
    62.     ReDim dirNames(nDir)
    63.     Cont = True
    64.     hSearch = FindFirstFile(path & "*", WFD)
    65.     If hSearch <> INVALID_HANDLE_VALUE Then
    66.         Do While Cont
    67.         DirName = StripNulls(WFD.cFileName)
    68.         ' Ignore the current and encompassing directories.
    69.         If (DirName <> ".") And (DirName <> "..") Then
    70.             ' Check for directory with bitwise comparison.
    71.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    72.                 dirNames(nDir) = DirName
    73.                 DirCount = DirCount + 1
    74.                 nDir = nDir + 1
    75.                 ReDim Preserve dirNames(nDir)
    76.             End If
    77.         End If
    78.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
    79.         Loop
    80.         Cont = FindClose(hSearch)
    81.     End If
    82.     ' Walk through this directory and sum file sizes.
    83.     hSearch = FindFirstFile(path & SearchStr, WFD)
    84.     Cont = True
    85.     If hSearch <> INVALID_HANDLE_VALUE Then
    86.         While Cont
    87.             FileName = StripNulls(WFD.cFileName)
    88.             If (FileName <> ".") And (FileName <> "..") Then
    89.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    90.                 FileCount = FileCount + 1
    91.                 List1.AddItem path & FileName
    92.             End If
    93.             Cont = FindNextFile(hSearch, WFD) ' Get next file
    94.         Wend
    95.         Cont = FindClose(hSearch)
    96.     End If
    97.     ' If there are sub-directories...
    98.     If nDir > 0 Then
    99.         ' Recursively walk into them...
    100.         For i = 0 To nDir - 1
    101.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
    102.         Next i
    103.     End If
    104. End Function
    105. Sub Command1_Click()
    106.     Dim SearchPath As String, FindStr As String
    107.     Dim FileSize As Long
    108.     Dim NumFiles As Integer, NumDirs As Integer
    109.     Screen.MousePointer = vbHourglass
    110.     List1.Clear
    111.     SearchPath = Text1.Text
    112.     FindStr = Text2.Text
    113.     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    114.     Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
    115.     Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
    116.     Screen.MousePointer = vbDefault
    117. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    Re: VB - List All The Files In A Directory

    Originally posted by manavo11
    This shows the filenames and not the total number of files in a directory.
    I'll take the easy route! Works a treat. Thanks.

    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  5. #5

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Re: VB - List All The Files In A Directory

    Originally posted by thebloke
    I'll take the easy route! Works a treat. Thanks.

    Anytime


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Junior Member
    Join Date
    Jan 2005
    Posts
    18

    Re: VB - List All The Files In A Directory

    Yup...this is nice, thank's

  7. #7
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Re: VB - List All The Files In A Directory

    Nice code example manavo11!

    Thanks, i need it!
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  8. #8
    Banned
    Join Date
    Mar 2005
    Posts
    26

    Re: VB - List All The Files In A Directory

    it works here.

  9. #9
    New Member
    Join Date
    Jun 2007
    Posts
    2

    Thumbs up Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub

    Hello,

    Thank for the above code, works a treat but I'm just wondering if anyone could help me out with a similarly easy way to do the same but to list the names of directories ("folders") within a directory?

    So, the above code works great, it lists all the files of whatever type in say "C:\folder1".

    What I need is code to list all the directories within "C:\folder1", not including subdirectories.

    If anyone could help I would be much obliged.

    Thanks in advance.

  10. #10
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by RedJam
    What I need is code to list all the directories within "C:\folder1", not including subdirectories.
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const LB_DIR = &H18D
    
    Private Const DDL_DIRECTORY = &H10
    Private Const DDL_ARCHIVE = &H20
    Private Const DDL_EXCLUSIVE = &H8000
    
    Private Sub Command1_Click()
        List1.Clear
        SendMessage List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_DIRECTORY, ByVal "C:\folder1\*.*"
    End Sub
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  11. #11
    New Member
    Join Date
    Jun 2007
    Posts
    2

    Re: VB - List All The Files In A Directory

    Cool, cheers mate

    Had to tweak it a bit of course but working now, thanks.

    J

  12. #12
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: VB - List All The Files In A Directory

    Here is my shot it gets all files in sub directories to!

    vb Code:
    1. Public Function ListFiles(FolderPath As String, Extension As String, LstObj As ListBox)
    2. Dim i As Long
    3. Dim FolderName As String
    4. Dim DirNames() As String
    5. Dim SubDirectories As Long
    6.  
    7. 'List files in the main/first folder
    8. FolderName = Dir(FolderPath & "\" & Extension, vbNormal)
    9.  
    10. Do While FolderName <> vbNullString
    11.     LstObj.AddItem FolderPath & "\" & FolderName
    12.     FolderName = Dir()
    13. Loop
    14.  
    15. 'Get the sub directories
    16. FolderName = Dir(FolderPath & "\*.*", vbDirectory)
    17.  
    18. Do While FolderName <> vbNullString
    19.     If FolderName <> "." And FolderName <> ".." Then
    20.         SubDirectories = SubDirectories + 1
    21.         ReDim Preserve DirNames(1 To SubDirectories)
    22.         DirNames(SubDirectories) = FolderName
    23.     End If
    24.         FolderName = Dir()
    25. Loop
    26.  
    27.  
    28. For i = 1 To SubDirectories
    29.         ListFiles FolderPath & "\" & DirNames(i), Extension, LstObj
    30. Next i
    31. End Function

    vb Code:
    1. Private Sub Command1_Click()
    2. ListFiles "C:\SomeFolder", "*.img", List1
    3. End Sub

    Sorry for the bump.

  13. #13
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: VB - List All The Files In A Directory

    just a note that none of the codes work in 2008.

    M.V.B. 2008 Express Edition

  14. #14

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - List All The Files In A Directory

    The Visual Studio 2008 you mean? It won't work, it's VB6 code. That's why it's in the VB6 and earlier codebank


    Has someone helped you? Then you can Rate their helpful post.

  15. #15
    New Member
    Join Date
    Aug 2009
    Posts
    1

    Re: VB - List All The Files In A Directory

    [QUOTE=Paul M;2973969]Here is my shot it gets all files in sub directories to!

    [QUOTE]

    Hi

    Apologies in advance for needing to ask a dumb question, but I'm pretty new to this and flying solo. I'm a tech writer, not a programmer, and have been making a few half-hearted attempts to learn VB so that I can have more control over my word docs. Problem is I am still at the starting blocks and need to catalogue all the template files in a directory, including all sub-dirs, so this post is very interesting to me, but I don't know what to do with the code!

    I know enough to open the VB editor in word, and paste it into a code window, but I don't know how to name it, or how to call it. Also, there's the question of the sub tacked on at the end and how to deal with it (where does it go, etc).

    Ok so banish me to the sandpit, but if someone would be kind enough to give me some steps on how to use this code first, I'll be extremely grateful. My other option is to hand type out several thousand file names.

    (PS While registering for the forum, I noticed an office forum, so i will head over there and read up, but this post is the first one i found covering exactly my required task)

  16. #16
    Member
    Join Date
    Sep 2008
    Location
    Turkey
    Posts
    37

    Re: VB - List All The Files In A Directory

    Thanks For aLL!

  17. #17
    New Member tamalero's Avatar
    Join Date
    Oct 2009
    Posts
    3

    Re: VB - List All The Files In A Directory

    Hello there, Thanks alot for your code,
    just a quick question...
    is there a way to list files in a directory but get the contents arranged in some ways like "last modified" or "by name" ?

  18. #18

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - List All The Files In A Directory

    I'm not sure if there is a way to get the list of files in a specific sorted order. However, since you can get all files, you can then read any information you want for each file (last modified, size, etc) and then do the sorting yourself after you get the whole list!


    Has someone helped you? Then you can Rate their helpful post.

  19. #19
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Unhappy List All The Files In A Directory and it's subdirs

    Hi I am making a search engine in VB6, your code works but I also need to list all files in subdirs as well. I already searched on google but I couldnt find anything. ( except the ones that cause your whole project to crash:S ) Do you know how to achieve this?

    Thanks in advance,
    Jason
    Last edited by jason8100; Dec 19th, 2009 at 03:52 PM. Reason: wrong quote

  20. #20

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - List All The Files In A Directory

    If you look at this thread: http://www.vbforums.com/showthread.php?t=244880 it's code for listing all sub-directories. If you combine the two, then you have exactly what you want


    Has someone helped you? Then you can Rate their helpful post.

  21. #21
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Unhappy Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    If you look at this thread: http://www.vbforums.com/showthread.php?t=244880 it's code for listing all sub-directories. If you combine the two, then you have exactly what you want
    I have tryed the code but don't get it working. It just gives a number of folders, don't know what to do with it. Could you please help me.

    Thanks in advance,
    Jason

  22. #22
    New Member
    Join Date
    Oct 2009
    Posts
    2

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub
    Quote Originally Posted by goldenix View Post
    just a note that none of the codes work in 2008.
    this code is pretty close, the following is what i turned it into and used, in vb.net 2008:

    Code:
    Dim file As String
            file = Dir$("D:\Documents and Settings\Compaq_Owner\My Documents\My Pictures\cards\*.png")
            Do While Len(file)
                ListBox2.Items.Add(Mid(file, 1, InStr(file, ".") - 1))
                file = Dir$()
            Loop

  23. #23
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: VB - List All The Files In A Directory

    cold we modify this for viewing the images?

  24. #24

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - List All The Files In A Directory

    You could specify an extension. It is mentioned in the examples if you read through them carefully.


    Has someone helped you? Then you can Rate their helpful post.

  25. #25
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    You could specify an extension. It is mentioned in the examples if you read through them carefully.
    well itryed just this but it shows nothing

    Dim file As String
    file = Dir$("D:\Documents and Settings\Compaq_Owner\My Documents\My Pictures\cards\*.png")
    Do While Len(file)
    ListBox2.Items.Add(Mid(file, 1, InStr(file, ".") - 1))
    file = Dir$()
    Loop

  26. #26

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - List All The Files In A Directory

    Try starting with the code from the third post: http://www.vbforums.com/showpost.php...79&postcount=3

    It seems like the most similar to the code you have. Compare it, find the differences and I'm sure you'll be able to work it out


    Has someone helped you? Then you can Rate their helpful post.

  27. #27
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub
    why do i get errors?
    i added a list box named it List1

    Error 1 Optional parameters must specify a default value. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 3 74 WindowsApplication1

    Error 2 'Public ReadOnly Property Right() As Integer' has no parameters and its return type cannot be indexed. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 7 12 WindowsApplication1

    Error 4 'Public Property Left() As Integer' has no parameters and its return type cannot be indexed. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 11 16 WindowsApplication1

    Error 5 Type character '$' does not match declared data type 'Integer'. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 11 16 WindowsApplication1

    Error 6 'AddItem' is not a member of 'System.Windows.Forms.ListBox'. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 17 13 WindowsApplication1

  28. #28
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,938

    Re: VB - List All The Files In A Directory

    You are getting errors because you are in the wrong place... this is the VB6 Codebank, but you are using VB.Net

    Post #22 is the only thing from this thread that you should consider using - for anything else (including help fixing issues with it) you should post in the VB.Net forum.

  29. #29
    New Member
    Join Date
    Mar 2011
    Posts
    15

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub
    I'm attempting to use this code, it should do everything I need, but my Listbox comes up blank. Which was my problem before and hoped this code would help me fix this. I'm teaching myself how to use VB6 so please spell things out to me as though I were a little kid. Thank you.

  30. #30

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by davis1d0 View Post
    I'm attempting to use this code, it should do everything I need, but my Listbox comes up blank. Which was my problem before and hoped this code would help me fix this. I'm teaching myself how to use VB6 so please spell things out to me as though I were a little kid. Thank you.
    Unless we see exactly your code, we won't be able to help! You can create a new thread in the Visual Basic 6 and Earlier forum to get more help


    Has someone helped you? Then you can Rate their helpful post.

  31. #31
    New Member
    Join Date
    Jul 2012
    Posts
    1

    Re: VB - List All The Files In A Directory

    vb Code:
    1. Public Shared Sub GET_ALL_FILES(ByVal Folder As String, ByVal List As ListBox)
    2.         For Each Files As String In My.Computer.FileSystem.GetFiles(Folder)
    3.             List.Items.Add(Files)
    4.         Next
    5.     End Sub
    There is my code. It work`s great!

  32. #32
    New Member
    Join Date
    Jul 2012
    Posts
    1

    Re: VB - List All The Files In A Directory

    how attribute first of the list to text1.text (as example)

    then delete first of the list

    and second of the list will be first ............ect

  33. #33
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    This shows the filenames and not the total number of files in a directory.
    Hi Manavo11,

    Could you please write this in VB5 code.

    Thanks

    Normale15

  34. #34
    Frenzied Member
    Join Date
    Dec 2008
    Posts
    1,188

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by normale15 View Post
    Hi Manavo11,

    Could you please write this in VB5 code.
    Thanks
    Normale15
    Here are a couple of links on how to do that -
    http://www.devx.com/vb2themax/Tip/18390
    http://www.vb-helper.com/vb6tovb5.htm
    Rob
    PS I hate people telling us to upgrade to VB.NET, however the upgrade from VB5 to VB6 is highly recommended (I won't clutter up this thread, with the reasons)
    Last edited by Bobbles; Feb 20th, 2017 at 04:21 AM.

  35. #35
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,448

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by normale15 View Post
    Hi Manavo11,

    Could you please write this in VB5 code.

    Thanks

    Normale15
    The sample code in post #2 and post #3 should also work in VB5.

  36. #36
    Lively Member
    Join Date
    Jul 2015
    Location
    Belarus, Minsk
    Posts
    98

    Re: VB - List All The Files In A Directory

    Maybe that module containing lots of FileSystem routines will be helpfull.
    Attached Files Attached Files

  37. #37
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: VB - List All The Files In A Directory

    Thanks to all that have replied.

    The folder structure I am working with is - drive letter/folder level1 (single folder)/folder level 2 (single folder)/folder level 3(multiple folders)/folder level 4 (multiple folders)/multiple filenames in each folder at level 4.

    What I am needing to do is to return, on a form, a list of all files showing part of the path (including the extension) but only that part of the path showing folder level 3, folder level 4, and the filename.

    The drive letter is not to be hard coded as the exe could be run from any drive letter.

    And of course using VB5

  38. #38
    Lively Member
    Join Date
    Jul 2015
    Location
    Belarus, Minsk
    Posts
    98

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by normale15 View Post
    Thanks to all that have replied.

    The folder structure I am working with is - drive letter/folder level1 (single folder)/folder level 2 (single folder)/folder level 3(multiple folders)/folder level 4 (multiple folders)/multiple filenames in each folder at level 4.

    What I am needing to do is to return, on a form, a list of all files showing part of the path (including the extension) but only that part of the path showing folder level 3, folder level 4, and the filename.

    The drive letter is not to be hard coded as the exe could be run from any drive letter.

    And of course using VB5
    You can modify DirSize function from my archive above to return an array of file and folders. And then filter/use it as you want. Or filter right "in place" before return.

  39. #39
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: VB - List All The Files In A Directory

    Sorry Hwoarang, I cannot find your DirSize archive. Where is it please?

  40. #40
    Lively Member
    Join Date
    Jul 2015
    Location
    Belarus, Minsk
    Posts
    98

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by normale15 View Post
    Sorry Hwoarang, I cannot find your DirSize archive. Where is it please?
    FileSystemFunctions.zip attached - 3 posts above. There is a VB6 project inside with a class that contains DirSize function. You can try to change it for your requirements - it should be pretty easy.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width