+ Reply to Thread
Results 1 to 6 of 6

Scan a Directory

  1. #1
    Registered User
    Join Date
    09-09-2005
    Posts
    2

    Scan a Directory

    Hi!
    I'd like to scan a directory and report all files present in it in a list box.
    I use this code (found on this wonderful site) but if I launch this macro excel goes in crash.
    Could you help me, please?

    With Application.FileSearch
    .NewSearch
    .LookIn = "c:\data\h3g"
    .Filename = "*.*"
    .SearchSubFolders = IncludeSubFolder
    .FileType = msoFileTypeAllFiles
    If .Execute(SortBy:=msoSortByFileName, _
    SortOrder:=msoSortOrderAscending) = 0 Then Exit Sub
    For FileCount = 1 To .FoundFiles.Count
    lb_Dash.AddItem (.FoundFiles(FileCount))
    Next FileCount
    .FileType = msoFileTypeExcelWorkbooks ' reset filetypes
    End With

  2. #2
    Registered User
    Join Date
    10-11-2004
    Posts
    18
    Here's piece of code I use;

    Dim FN As String ' For File Name
    Dim ThisRow As Long
    Dim FileLocation As String
    FileLocation = "c:\data\h3g"
    FN = Dir(FileLocation)
    Do Until FN = ""
    ListBox2.AddItem FN
    FN = Dir
    Loop


    Lenny

  3. #3
    Registered User
    Join Date
    09-09-2005
    Posts
    2
    Very useful!!!! Thanks a lot!

  4. #4
    Bob Umlas
    Guest

    Re: Scan a Directory

    Looks like you're missing lots of items beginning with ".", like:
    ..NewSearch
    ..Lookin=...
    ..Filename=...
    , etc
    "jjresti" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hi!
    > I'd like to scan a directory and report all files present in it in a
    > list box.
    > I use this code (found on this wonderful site) but if I launch this
    > macro excel goes in crash.
    > Could you help me, please?
    >
    > With Application.FileSearch
    > NewSearch
    > LookIn = "c:\data\h3g"
    > Filename = "*.*"
    > SearchSubFolders = IncludeSubFolder
    > FileType = msoFileTypeAllFiles
    > If .Execute(SortBy:=msoSortByFileName, _
    > SortOrder:=msoSortOrderAscending) = 0 Then Exit Sub
    > For FileCount = 1 To .FoundFiles.Count
    > lb_Dash.AddItem (.FoundFiles(FileCount))
    > Next FileCount
    > FileType = msoFileTypeExcelWorkbooks ' reset filetypes
    > End With
    >
    >
    > --
    > jjresti
    > ------------------------------------------------------------------------
    > jjresti's Profile:
    > http://www.excelforum.com/member.php...o&userid=27103
    > View this thread: http://www.excelforum.com/showthread...hreadid=466189
    >




  5. #5

    Re: Scan a Directory

    Try:

    With Application.FileSearch
    .LookIn = "C:\My Documents"
    .Filename = "*"
    If .Execute > 0 Then
    For x = 1 To .FoundFiles.Count
    tFilename = .FoundFiles(x)
    Lb_Dash.AddItem (tFilename)
    Next x
    Else
    MsgBox "No Files"
    End If
    End With

    HTH


  6. #6
    Arvi Laanemets
    Guest

    Re: Scan a Directory

    Hi

    I'll copy some my UDF's here - I think they can give you some ideas (the
    optional parameter MyTime allows to use UDF's as volatile)

    ----
    Public Function GetSubfolder(MyFolder As String, FolderNum As Integer,
    Optional MyTime As Date)
    Dim fs, f, f1, s, sf
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(MyFolder)
    Set sf = f.SubFolders
    i = 0
    For Each f1 In sf
    i = i + 1
    If i = FolderNum Then GetSubfolder = f1.Name
    Next
    End Function

    Public Function GetFileName(MyFolder As String, FileNum As Integer,
    MyExtension As String, Optional MyTime As Date)
    Dim fs, f, f1, fc, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(MyFolder)
    Set fc = f.Files
    i = 0
    For Each f1 In fc
    If Right(f1.Name, 3) = MyExtension Then
    i = i + 1
    If i = FileNum Then GetFileName = f1.Name
    End If
    Next
    End Function


    Public Function GetThisFolder(Optional MyTime As Date)
    GetThisFolder = ThisWorkbook.Path
    End Function


    --
    Arvi Laanemets
    ( My real mail address: arvil<at>tarkon.ee )


    "jjresti" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hi!
    > I'd like to scan a directory and report all files present in it in a
    > list box.
    > I use this code (found on this wonderful site) but if I launch this
    > macro excel goes in crash.
    > Could you help me, please?
    >
    > With Application.FileSearch
    > NewSearch
    > LookIn = "c:\data\h3g"
    > Filename = "*.*"
    > SearchSubFolders = IncludeSubFolder
    > FileType = msoFileTypeAllFiles
    > If .Execute(SortBy:=msoSortByFileName, _
    > SortOrder:=msoSortOrderAscending) = 0 Then Exit Sub
    > For FileCount = 1 To .FoundFiles.Count
    > lb_Dash.AddItem (.FoundFiles(FileCount))
    > Next FileCount
    > FileType = msoFileTypeExcelWorkbooks ' reset filetypes
    > End With
    >
    >
    > --
    > jjresti
    > ------------------------------------------------------------------------
    > jjresti's Profile:
    > http://www.excelforum.com/member.php...o&userid=27103
    > View this thread: http://www.excelforum.com/showthread...hreadid=466189
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1