processes multiple stored files : File « File Path « VBA / Excel / Access / Word






processes multiple stored files

 
Sub BatchProcess()
    Dim Files() As String
    Dim FileSpec As String
    FileSpec = "c:\text.txt"

    NewPath = ExtractPath(FileSpec)
    FoundFile = Dir(FileSpec)
    If FoundFile = "" Then
        MsgBox "Cannot find file:" & FileSpec
        Exit Sub
    End If

    FileCount = 1
    ReDim Preserve Files(FileCount)
    Files(FileCount) = FoundFile

    Do While FoundFile <> ""
        FoundFile = Dir()
        If FoundFile <> "" Then
            FileCount = FileCount + 1
            ReDim Preserve Files(FileCount)
            Files(FileCount) = FoundFile
        End If
    Loop

    For I = 1 To FileCount
        Application.StatusBar = "Processing " & Files(I)
        Call ProcessFiles(Files(I))
    Next I
    Application.StatusBar = False
End Sub

Sub ProcessFiles(FileName As String)
    Workbooks.OpenText FileName:=FileName, Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(3, 1), Array(12, 1))
End Sub
Function ExtractPath(Spec As String) As String
    SpecLen = Len(Spec)
    For I = SpecLen To 1 Step -1
        If Mid(Spec, I, 1) = "\" Then
            ExtractPath = Left(Spec, I - 1)
            Exit Function
        End If
    Next I
    ExtractPath = ""
End Function

 








Related examples in the same category

1.Does the specified file exist?
2.Check file existance
3.Gets the file name from the path.
4.List all files under application path
5.Select a folder
6.Select a location containing the files you want to list
7.Common File Operations Simplified
8.Use array to store a list of files