Loop through files in a folder

In writing Dynamics integrations, I'm frequently asked to read a directory of files and process them somehow. Sometimes they're XML, sometimes text. 

This piece of code is the main loop for that task. It's not ground breaking or brilliant; but it saves me 30-60 minutes of coding to have it ready so that I can copy and paste into an app. 

The basic logic is this:

Create 'done' and 'error' folders to move the files into after processing

Loop

Process the file

Move the file to 'done'

Trap errors

Move the file to 'error

Handle the error 

Imports System.IO
Imports System.Xml
Imports FPCommon
 
Public Class Main
    Dim _bShowErrors As Boolean
 
    'this class is designed to bubble events up to the calling class
    'the calling class can subscribe to the events and display progress
    Public Event Status(ByVal StatusText As String)
 
 
    Sub New(bShowErrors As Boolean)
        Try
            _bShowErrors = bShowErrors
            'the app class is here: http://dyndeveloper.com/files/fpcommon.zip
            App.ReadConfigFile("Credit Plus", "DDIntegration.config")
 
        Catch ex As Exception
            'the globalErrorHandler method is here: http://dyndeveloper.com/files/fpcommon.zip
            ErrorHandler.globalErrorHandler(ex, _bShowErrors)
        End Try
    End Sub
    Public Sub Main()
        work()
    End Sub
 
    Sub work()
        Try
            Dim strArchiveName As String = ""
            Dim intDeleteFilesOrderThanDays As Int16 = App.DeleteFilesOlderThanDays
 
            'get the root directory
            Dim strDirectory As String = App.RootDirectory
 
            'the done and error directories will be subdirectories
            Dim strDirectoryDone As String = Path.Combine(strDirectory, "done")
            Dim strDirectoryError As String = Path.Combine(strDirectory, "error")
 
            'create the sub directories if they do not exist
            'the FileHandler class is here: http://dyndeveloper.com/files/fpcommon.zip
            FileHandler.CreateDirectory(strDirectoryDone)
            FileHandler.CreateDirectory(strDirectoryError)
 
            'delete saved documents
            'the FileHandler class is here: http://dyndeveloper.com/files/fpcommon.zip
            FileHandler.DeleteDocumentsByAge(strDirectoryDone, intDeleteFilesOrderThanDays)
 
            'main work loop
            Dim oFiles As New DirectoryInfo(strDirectory)
            For Each oFile As FileInfo In oFiles.GetFiles("*.*")
                Try
                    'get the 'archivename'
                    strArchiveName = String.Format("{0}_{1:ddMMyy_hhmmss}{2}", Path.GetFileNameWithoutExtension(oFile.Name), Now, Path.GetExtension(oFile.Name))
                    'process one document  
                    Process(oFile.Name)
 
                    'move the file to done
                    FileHandler.MoveFile(oFile.FullName, Path.Combine(strDirectoryDone, strArchiveName))
                Catch ex As Exception
                    'it's possible to get an exception in the exception handler
                    'so we create a list of exceptions
                    Dim exList As New List(Of Exception)
                    exList.Add(ex)
 
                    Try
                        File.Move(oFile.FullName, Path.Combine(strDirectoryError, strArchiveName))
                    Catch ex2 As Exception
                        exList.Add(ex2)
                    End Try
 
                    'the globalErrorHandler class is here: http://dyndeveloper.com/files/fpcommon.zip
                    ErrorHandler.globalErrorHandler(exList, _bShowErrors)
                End Try
            Next
 
        Catch ex As Exception
            'the globalErrorHandler class is here: http://dyndeveloper.com/files/fpcommon.zip
            ErrorHandler.globalErrorHandler(ex, _bShowErrors)
 
        End Try
 
    End Sub
 
    Sub Process(ByVal strDocument As String)
        Try
            'do some work here to process the document
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
 
End Class
 

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences