Find out if a file is open in another program

If you have a process which fails when a file is open and locked in another program you can use PowerShell in exMon to monitor and notify you when that happens.

1. Create a new Query in exMon Administrator by right clickin the Tests folder

2. Select the PowerShell Data Provider

3. Add the following PowerShell script to the Query window:

# The file to check for
$path = ‘C:\temp\test.xlsx’

# Check if we can get an exclusive read on a file
# Returns the exception if unsuccessful
# Returns empty results if it reads successfully

# Create the result DataTable
$exMonResult= New-Object system.Data.DataTable
$col1 = New-Object system.Data.DataColumn Error,([string])

# Read the file
$text = $null
try {
    $file = []::Open($path, ‘Open’, ‘Read’, ‘None’)
    $reader = New-Object System.IO.StreamReader($file)
    $a = $reader.ReadLine()
} catch [Exception] { $text = $_.Exception.Message }

# Succeeded?
if($text -eq $null) {
    # Yes, no error message
} else {
    # Add Row
    $row = $exMonResult.NewRow();
    $row.Error = $text;

4. Change the $path variable to the file to monitor.

5. Configure email settings and schedule as normal.

exMon Contact Us