Consulting

Results 1 to 3 of 3

Thread: Open different files in a folder

  1. #1
    VBAX Regular
    Joined
    Dec 2007
    Posts
    45
    Location

    Open different files in a folder

    Hi,

    Is there anyway of writing a bit of code that will look in folder, say called C\test\results\...
    Then within this folder there may be other sub folders and files but ignore these and just look for those files which have been dropped there yesterday.
    For example I may have two files called:
    cpcfiegbt010108a.csv
    cpcfiegbs010108a.csv
    As you will see both created on the 1st Jan 2008 (010108).
    I then want to open these files.
    New files will be added so for example on the 3rd Jan 2008 there will be:
    cpcfiegbt020108a.csv
    cpcfiegbs020108a.csv
    I need to be able to open the 2nd Jan 2008 files and not the 1st Jan 2008 files and so on for each day.
    Can this be done?

    Many Thanks

    Steve

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


    Sub LoopFolders()
    Dim oFSO As Object
    Dim Folder As Object
    Dim Files As Object
    Dim file As Object

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    Set Folder = oFSO.GetFolder("c:\Test")

    For Each file In Folder.Files
    If file.Type Like "*Comma Separated*" Then
    If file.Name Like "*" & Format(Date - 1, "ddmmyy") & "*" Then
    Workbooks.Open Filename:=file.Path
    'process it
    ActiveWorkbook.Close
    End If
    End If
    Next file

    Set Folder = Nothing
    Set oFSO = Nothing

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Dec 2007
    Posts
    45
    Location
    XLD, thanks a million, helped alot and got me going.

    Steve


Posting Permissions

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