Powershell Filesystemwatcher run batch file on file created

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 2 months, 2 weeks ago.

  • Author
    Posts
  • #95913

    Steve
    Participant

    I have done much searching and have found the following code that allows me to log to the console and write to a log file. I am only tracking file creation and deletion.
    Upon file creation I would also like to execute a batch file.
    The code works fine until I add the line to execute the batch file in the Filecreated action block.
    the code to log:

    $folder = 'c:\temp' # Enter the root path you want to monitor.
    $filter = '*.*'  # You can enter a wildcard filter here.
    
    # In the following line, you can change 'IncludeSubdirectories to $true if required.                          
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
    
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
    Out-File -FilePath c:\temp\log\Filelog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
    
    Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore red
    Out-File -FilePath c:\temp\log\Filelog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
    

    As I said this works fine.
    once I add the batch file line which I found in this forum the Filecreated action block does not work.
    I don't get the messages or the batch file does not execute.
    If I delete a file that still works.
    The code to log and run batch:

    $folder = 'c:\temp' # Enter the root path you want to monitor.
    $filter = '*.*'  # You can enter a wildcard filter here.
    
    # In the following line, you can change 'IncludeSubdirectories to $true if required.                          
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
    
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
    Out-File -FilePath c:\temp\log\Filelog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
    c:\temp\runbatch.bat
    }
    
    Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore red
    Out-File -FilePath c:\temp\log\Filelog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
    

    Any help would be appreciated. I am new to Powershell and by no means a script person.
    The above code I have found all over the internet but for the life of me I cannot get it to execute the batch file.

  • #95961

    Don Jones
    Keymaster

    The problem is that running a batch file will run Cmd.exe, which winds up being out-of-process for PowerShell. I'm not sure how you'd get that to run in response to a .NET event. Try sticking entirely with PowerShell, if you can, instead of running an external batch file.

You must be logged in to reply to this topic.