Results 1 to 5 of 5

Thread: open files in folder sequencially

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    50

    open files in folder sequencially

    I have a folder that can contain any number of files.

    I want to open each one pull some data from it onto a master spreadsheet, save it in a sub folder and then close the saved file and delete the original file.

    I am confident I can write the code saving file in subfolder and deleting original, where I am struggling is the code for opening non specified files until there are no more.


    Thanks in advance.

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: open files in folder sequencially

    Mikeymay:
    This is one way to scan a directory:
    Code:
    Sub Macro1()
    Dim tStr As String
    
    ' Fetch the first filename, if any
    tStr = Dir("C:\Table-X\*.*")  'See Dir in the HelpHeap
    While Len(tStr) > 0
        'Do something with the file HERE
        Debug.Print tStr  'Show the Filename in the Immdeiate window
        '
        tStr = Dir    'Fetch the next file
    Wend
    
    End Sub
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: open files in folder sequencially

    Here is an alternative:


    VB Code:
    1. Set fs = CreateObject("Scripting.Filesystemobject")
    2. Set f = fs.GetFolder("c:\")
    3. counter = 1
    4. For Each i In f.Files
    5.     Cells(counter, 1) = i
    6.     counter = counter + 1
    7. Next

    Obviously you don't need the counter and can replace the Cells line with whatever you wish to do with the file. Set the path in the second line, or use a dialogbox to let the user select it.

    zaza

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: open files in folder sequencially

    zaza took a lot of shorthand shortcuts ... here is the full version:
    Code:
    Option Explicit
    Sub Macro1()
    Dim counter As Integer
    'Requires "Microsoft Scripting Runtime" to be checked in Tools > References
    Dim fso As Scripting.FileSystemObject
    Dim aFolder As Scripting.Folder
    Dim aFile As Scripting.File
    
    Set fso = CreateObject("Scripting.Filesystemobject")
    Set aFolder = fso.GetFolder("c:\")
    counter = 1
    For Each aFile In aFolder.Files
        Cells(counter, 1).Value = aFile.Name
        counter = counter + 1
    Next aFile
    
    End Sub
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    50

    Re: open files in folder sequencially

    Think I have enought there to help me along, thanks!

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