# Directory Watch

The Directory Watch is able to monitor a folder and report back when a new file is present. It has several key checks in place to make sure the file isn't actively being written too, or locked by another application before executing the file ready callback.

Directory Watch expects that the file will be removed from the directory after it has been processed. For this reason there are several options on the DirectoryWatchFailurePolicy on how to handle when a file hasn't been removed.&#x20;

{% hint style="success" %}

* Directory Watch is great for file processing where the file is expected to be removed.
* [Directory Notifier](/core-modules/event-sources/watchers/directory-notifier.md) is great for supporting hot-reload at runtime.&#x20;
  {% endhint %}

### Demo Application

This demo will:

* Watch the `C:\Watch` folder
* It will report on any CSV files (`*.csv`)
* It will search `AllDirectories` (subDirectoties) as well as it's root
* The `DirectoryWatchFailurePolicy` is set to move the file to a **`_Failed`** folder if the file has not been moved or deleted after the callback has completed.&#x20;

As a bonus, we'll [read that CSV data](/core-modules/file-formats/csv.md) in and report on it's load.

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

    c.AddDirectoryWatch("CSV", "C:\\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);

});
```

#### Dummy sample file used here:

When the above example run's against this file, it will log:

```log
Read CSV NewCSV.csv[US-ASCII]. Columns/Rows: 2/2; Delimiter: ','; Jagged? NO
```

{% file src="/files/rnbv5tSaUgBcVzcWLK5F" %}

### Failure Policies

At the moment, Perigee contains 3 different failure policies:

* Redelivery after a certain period of time
* Move to Failed Folder (\_Failed)
* Delete removes the file from the system.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.perigee.software/core-modules/event-sources/watchers/directory-watch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
