Page cover image

Watch and load CSVs

This example uses a file watcher to scan for CSV's and parse them into a DataTable. It prints the info about it.

PerigeeApplication.ApplicationNoInit("Watcher Demo", (c) =>
{

    c.AddDirectoryWatch("CSV", "C:\\temp\\Watch", "*.csv", SearchOption.AllDirectories, (ct, l, path) => {

        //Read the CSV
        var CSVData = CSVReader.ToDataTable(path, out var rRes);

        //Reprt on it
        l.LogInformation("Read CSV {file}[{encoding}]. Columns/Rows: {col}/{row}; Delimiter: {delChar}; Jagged? {jagged}", 
            Path.GetFileName(path), rRes.FileEncoding.EncodingName, rRes.ColumnCount, 
            CSVData.Rows.Count, rRes.FinalDelimiter, rRes.RowShifts.Count > 0 ? "YES" : "NO");

        //You'll notice the file gets moved to the _Failed Folder (Due to DirectoryWatchFailurePolicy supplied below)
        //  Watcher expects the file to be removed after it's processed to prevent infinite loops


    }, policy: ThreadRegistry.DirectoryWatchFailurePolicy.MoveToFailedFolder);

});

Last updated