PDA

View Full Version : Open different files in a folder



obriensj
01-02-2008, 09:44 AM
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

xld
01-02-2008, 10:33 AM
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

obriensj
01-03-2008, 04:01 AM
XLD, thanks a million, helped alot and got me going.

Steve

:clap: