+ Reply to Thread
Results 1 to 3 of 3

Import Folder names (1st level only) into excel

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2011
    Location
    London, england
    MS-Off Ver
    Excel 2003
    Posts
    23

    Import Folder names (1st level only) into excel

    Hey guys,

    So i'm trying to import a list of folder names but only 1st level and not any subfolders - make sense?
    So I got some wicked code from the net that imports folder names and subfolders however I only want to import the lst level of folder names:

    Sub ListThem()
       Dim startRange As Range
     
      Sheet1.Cells.Clear
      Set startRange = Sheet1.Range("A1")
      'Parent Directory - Change this to whichever directory you want to use
      ListFoldersAndInfo "C:\Documents and Settings\Administrator\My Documents", startRange
    End Sub
     
     
    Sub ListFoldersAndInfo(foldername As String, Destination As Range)
      Dim FSO As Object
      Dim Folder As Object
      Dim R As Long
      Dim SubFolder As Object
      Dim Wks As Worksheet
     
        Set FSO = CreateObject("Scripting.FileSystemObject")
     
          Set Folder = FSO.GetFolder(foldername)
          Destination = Folder.Name
          Destination.Offset(0, 1) = Folder.Path
          Destination.Offset(0, 2) = Folder.Size
          Set Destination = Destination.Offset(1, 0)
     
          For Each SubFolder In Folder.SubFolders
             ListFoldersAndInfo Folder.Path & "\" & SubFolder.Name, Destination
          Next SubFolder
     
        Set FSO = Nothing
     
    End Su
    P.S if the code was taken from:
    http://www.excelforum.com/excel-prog...-in-excel.html

    Thanks

    Wido
    Last edited by widodude; 06-20-2011 at 09:53 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Import Folder names (1st level only) into excel

    How about this?

    Sub ListThem()
       Dim startRange As Range
     
      Sheet1.Cells.Clear
      Set startRange = Sheet1.Range("A1")
      'Parent Directory - Change this to whichever directory you want to use
      ListFoldersAndInfo "C:\Documents and Settings\Administrator\My Documents", startRange
    
    End Sub
     
     
    Sub ListFoldersAndInfo(foldername As String, Destination As Range)
      
      Dim FSO As Object
      Dim Folder As Object
      Dim R As Long
      Dim SubFolder As Object
      Dim Wks As Worksheet
     
        Set FSO = CreateObject("Scripting.FileSystemObject")
     
          Set Folder = FSO.GetFolder(foldername)
     
          For Each SubFolder In Folder.SubFolders
            Destination = SubFolder.Name
            Destination.Offset(0, 1) = SubFolder.Path
            Destination.Offset(0, 2) = SubFolder.Size
            Set Destination = Destination.Offset(1, 0)
          Next SubFolder
     
        Set FSO = Nothing
     
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    01-11-2011
    Location
    London, england
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Import Folder names (1st level only) into excel

    That's just perfect. Thanks!

+ 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