tags:

views:

45

answers:

3

Hi, I have a number of excel (.xls) stored in a folder in a local drive. I need to do some process to every file in this folder. What is the code that will

  1. loop through every file
  2. open the file
  3. Do some processing and then Save & close the file
  4. move the file to another folder after processing

To be more clear, I want go over every file and do processing to it. After finishing a file, go to another file and so till the end of all the files in the folder. I do have the code for the processing; I just need to know the code that will loop through the files and move then to another folder.

Thanks for your help in advance,

A: 

What you need is a recursive function that iterates over the tree that represents a file system. It means to iterate over all the childs of some 'parent folder'. I send you a function that does something similar, to the one you need (this is currently in usage). This function deletes all the empty folders given a parent folder.

Public Function gf_DeleteEmptyFolder(path As String) As Boolean
    On Error GoTo Error_Handler    
    Dim fso_folder As Scripting.Folder, sub_folder As Scripting.Folder    
    If g_FSO.FolderExists(path) Then
        Set fso_folder = g_FSO.GetFolder(path)
        '-- eliminates de folder only if is empty
        If 0 = fso_folder.Files.Count And 0 = fso_folder.SubFolders.Count Then
            Call g_FSO.DeleteFolder(path, False)
        '-- recursively calls the function
        Else
            For Each sub_folder In fso_folder.SubFolders
                Call gf_DeleteEmptyFolder(sub_folder.path)
            Next
        End If
    End If
    gf_DeleteEmptyFolder = True    
    Exit Function
'~~~ on error
Error_Handler:
    gf_DeleteEmptyFolder = False
End Function

If your files are stored in a simple folder, then you can use the following code to iterate each file.

Public Sub fsoProcessFilesInFolder(sFolder As String)        
    Dim fso As Scripting.FileSystemObject, fld As Scripting.Folder, fil As Scripting.File    
    Set fso = New FileSystemObject
    Set fld = fso.GetFolder(sFolder)    
    For Each fil In fld.Files
        '--- add code to process your files
    Next fil
End Sub
ArceBrito
yes, all files are in one simple folder. let me try the code you gave me. thanks!
guest1
what is passed to the function as sFolder??? and don't we need the dir of the folder?
guest1
by the way, I am using VB6
guest1
I got the solution! thanks for your help though!
guest1
@guest1: ok, that is the idea..
ArceBrito
A: 

Here's the easy VBA object way to do it:

Dim fs As FileSearch
Dim i As Integer
Dim wbk As Workbook

Set fs = Application.FileSearch

With fs
    .LookIn = ThisWorkbook.Path
    .FileName = "*.xls"
    For i = 1 to .Execute()
        Set wbk = Workbooks.Open(.FoundFiles(i))
        ''//DO STUFF HERE
        wbk.Close(SaveChanges:=True)
    Next i
End With

In VB6 you have three options, as shown in the following KB articles:

How to Search Directories to Find or List Files
HOW TO: Recursively Search Directories by Using FileSystemObject

Lance Roberts
Thanks for the reply. What reference I need to add for the FileSearch datatype. It gives me an error
guest1
@guest1, I guess that might depend on your environment, I'm using Excel 2003, and I didn't have to add a reference, though it's possible that it's already there. MS says that you need the `Microsoft Office Object Library` reference.
Lance Roberts
I am using VB6 though
guest1
@guest1, that might take me a while to figure out, in fact it might not be possible in VB6. I was using Excel VBA (which you also tagged the question with). In .net you'd use the `Microsoft.Office.Core` namespace. I'll keep digging.
Lance Roberts
@Lance Roberts, Thanks buddy!
guest1
@guest1, ok, you can't do it in VB6 that way, but I edited in links to the Knowledge Base articles that give all the gory details on how to get it done in VB6. Good luck.
Lance Roberts
I got the solution! thanks for your help though!
guest1
A: 

Following code will read xlsx/xls files from given folder neglecting other files and iterate through each item. You can use it for any set of extensions and filters.

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(folderPath)

Set objFiles = objFolder.Files

'Iterate through the files in the folder

For Each Item In objFiles

If LCase(Right(Item.Name, 5)) = ".xls" Or LCase(Right(Item.Name, 4)) = ".xlsx" Then

 ''''''Do Stuffs Here''''''

End If

Next

expressions