Results 1 to 17 of 17

Thread: [RESOLVED] get all filenames in a drive

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    52

    Resolved [RESOLVED] get all filenames in a drive

    hi, can anyone help me in getting all the list of filenames in a drive(ex flash drive, drive e:\ for example) i used the file list box but is there and alternative where i can filter the files like getting all the list of filenames which files are .doc for example.,? and can i list all the filenames and put it in the listbox?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: get all filenames in a drive

    Look up the Dir() command.... on the initial call, pass it the filter you want.... then start a loop as long as you have a file name returned.... add it to the lsit box... then keep calling Dir() without any paramters, until nothing is returned, adding to the list box as you go.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    52

    Re: get all filenames in a drive

    sir thx for the reply., sir i heard about dir but i new here in vb so im not very familiar about the coding in dir() sir can you guide me about the dir? and do you have sample code or syntax?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: get all filenames in a drive

    Look up the Dir() Command
    The Dir() Entry on MSDN
    the code sample is .NET, but it's in the Vb6 syntax/format, so with a couple of minor tweaks, it'll run jsut fine.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: get all filenames in a drive

    do a search on this forum for recursive dir, there will be many examples, alternatives are FSO and findfirstfile /findnextfile APIs, there will be many examples of these too
    recursive is when you search all subdirectories as well
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Re: get all filenames in a drive

    try:
    Code:
    'Command Button And Listbox
    
    Private Sub Command1_Click()
    
    Dim sFiles  As String
    
    sFiles = Dir("C:\*.doc")
    
    Do While Len(sFiles) > 0
        List1.AddItem sFiles
        sFiles = Dir
    Loop
    
    End Sub
    "More Heads are Better than One"

  7. #7
    Lively Member TechNix's Avatar
    Join Date
    Nov 2008
    Location
    Manila
    Posts
    69

    Re: get all filenames in a drive

    Also try this with API:

    Code:
    Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" _
                            (ByVal hwnd As Long, ByVal wMsg As Long, _
                             ByVal wParam As Long, ByVal lParam As String) As Long
      
          Private Sub Command1_Click()
              Dim lngF As Long
              lngF = SendMessageStr(List1.hwnd, &H18D, &H20, "C:\*.*")
          End Sub

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    52

    Re: get all filenames in a drive

    thx guys.., cheers to all!

  9. #9
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: [RESOLVED] get all filenames in a drive

    Add a list box and a command button to a form. Then apply this code:
    Code:
    Private Sub Command1_Click()
    Dim NameFile As String, SubDir As String
    SubDir = CurDir$ & "\*.*" ' Change CurDir to whatever directory is being searched
    NameFile = Dir$(SubDir)
    Do While NameFile <> vbNullString
        List1.AddItem NameFile
        NameFile = Dir$
    Loop
    End Sub
    Doctor Ed

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    52

    Re: [RESOLVED] get all filenames in a drive

    sir thx for the code it works
    just one thing., if i change the curdir to D$ it doesnt change the directory. its still on the drive C

  11. #11
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [RESOLVED] get all filenames in a drive

    Quote Originally Posted by huxley View Post
    sir thx for the code it works
    just one thing., if i change the curdir to D$ it doesnt change the directory. its still on the drive C
    CurDir is a VB Function.

    Syntax: CurDir[(drive)]
    The optional drive argument is a string expression that specifies an existing drive. If no drive is specified or if drive is a zero-length string (""), CurDir returns the path for the current drive.


    If you want to hard code in the drive and path then just change the SubDir string to what you want. i.e. the root of drive D:, SubDir = "D:\*.*"

  12. #12
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: [RESOLVED] get all filenames in a drive

    Quote Originally Posted by Edgemeal View Post
    CurDir is a VB Function.

    Syntax: CurDir[(drive)]
    The optional drive argument is a string expression that specifies an existing drive. If no drive is specified or if drive is a zero-length string (""), CurDir returns the path for the current drive.

    If you want to hard code in the drive and path then just change the SubDir string to what you want. i.e. the root of drive D:, SubDir = "D:\*.*"
    Thanks, Edge. You got there faster than I did. (Longer lunch hour than expected.)
    Doctor Ed

  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    52

    Re: [RESOLVED] get all filenames in a drive

    thx guys., it works., cheers


    is it possible to identify the folders using the DIR function?

  14. #14
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Re: [RESOLVED] get all filenames in a drive

    Code:
    If Dir("C:\WINDOWS", vbDirectory) <> vbNullString Then
        MsgBox "Folder Exist"
    End If
    "More Heads are Better than One"

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    52

    Re: [RESOLVED] get all filenames in a drive

    im mean sir is it possible also for the folders to be listed just like the files listed in the listbox.

  16. #16
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Re: [RESOLVED] get all filenames in a drive

    Last edited by jp26198926; Jun 9th, 2009 at 07:14 AM.
    "More Heads are Better than One"

  17. #17

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    52

    Re: [RESOLVED] get all filenames in a drive

    thank you sir., this post is resolved., thank you for all the reply., cheers!

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