Consulting

Results 1 to 10 of 10

Thread: picking up file names in one folder

  1. #1

    picking up file names in one folder

    Hi!
    I want to pick all names of files with different extensions in one excel sheet. Those files are in same folder with my Excel file. How can i do this? Please help me!
    Thanks in advance
    SureshP

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Have a look at the Dir function in VBA help.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Hi!
    I am very new to VBA, so it will be more helpful to me if you give me one example.
    Thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub Test()
    Dim sFile As String
    Dim i As Long

    i = 1
    sFile = Dir("C:\Projects\Temp\*.*") '<=== change to suit
    Do While sFile <> ""
    Cells(i, "A").Value = sFile
    On Error Resume Next
    sFile = Dir
    On Error GoTo 0
    i = i + 1
    Loop

    End Sub

    [/vba]

  5. #5
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Same things as dir, using fso.

    [VBA]
    Dim FSO As object, f As object, Path As String
    Set FSO = CreateObject("Scripting.FileSystemObject")
    For Each f In FSO.GetFolder("C:\").Files
    ActiveSheet.Range("a" & Application.Rows.Count).End(xlUp).Offset(1, 0) = f.Name
    Next f
    [/VBA]

    Although my code is getting far more files for some reason? Maybe I'm picking up hidden and system files as well? Not sure.
    The most difficult errors to resolve are the one's you know you didn't make.


  6. #6
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    I did a check, and my code is extracting hidden and systems files, which it seems Bob's code is not. Shouldn't be an issue in a user folder though, either way, but something to keep in mind.

    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If files are hidden, and a directory listing is set to not show system files, it could be argued that they shouldn't be picked up, after all it is either company or user policy.

  8. #8
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Bob,
    No aurgement from me, just something I thought should be pointed out to the OP, so they can make an educated decision on which way to go.

    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Nor me, just ruminating out aloud.

  10. #10
    Hi!
    Thanks to Bob and Cal and all who helped me to do this! thankyou very much

Posting Permissions

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