Batch Processing Word for Windows Files

In preparing text files we often need to perform the same set of routines on all of the files in a particular directory. The following WordBasic routine will process all of the files in the directory that contains the active document. It opens each file, determines it's extension and takes action contingent upon the extension.


Sub MAIN

Dim rec As DocumentStatistics
Dim fnames$(500)

'Find the dir of the active document
GetCurValues rec
docpath$ = rec.Directory + "\*.*"

'Fill an array with the names of all files 
currfile$ = LCase$(Files$(docpath$))
While currfile$ <> ""
    fnames$(fcount) = currfile$
    fcount = fcount + 1
    currfile$ = LCase$(Files$())
Wend

'for each file in the array...
For j = 0 To fcount - 1
    FileOpen .Name = fnames$(j)

    'Get the file extension
    filetype$ = LCase$(Right$(fnames$(j), 3))

    'Do processes contingent on the extension
    Select Case filetype$
    Case "doc"
        '<Code>
    Case "txt"
        '<Code>
    Case Else
    End Select
    
    'Close the file without saving (it must be saved above)
    FileClose 2
Next j
End Sub