I'd like to monitor a Windows 7 folder and have a .bat file run when any new files are added to the folder. It seems like I should be able to do this using powershell, which is installed on the computer.
I've read some answers like this one but I'm not able to get anything to work yet by modifying what I see. Details:
Folder to monitor:
c:\aaa\bbb\monitorThis
Batch file to run whenever an .htm file is added to the monitored folder:
c:\aaa\bbb\runA.bat
Powershell script file:
c:\aaa\bbb\folderWatcher.ps1
Can someone describe what the content of folderWatcher.ps1 should look like, including the line containing the command to run the .bat file, registering and unregistering the event, and so on?
Also, is right-clicking the .ps1 file and selecting "Run with PowerShell" the way to start the monitoring, and if so, how do you stop it?
UPDATE:
As requested, here is what I have so far for folderWatcher.ps1, but it's just a start, from ideas I've seen:
$folder = "c:\aaa\bbb\toConvert"
$filter = "*.*"
$fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubDirectories=$false
NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
Start-Process cmd -ArgumentList "/c runA.bat" -WorkingDirectory "C:\aaa\bbb"
}
Note: re filtering, I don't care what kind of file is added, since we will only be putting .htm files in that folder, so anything added to it I want to trigger the .bat.
UPDATE II
I tried the code from Dennis below but I get nothing. I just double-checked all my paths to be sure they were the equivalent of what he has. I also just made a new test version with simpler paths so I can post exactly what I have without having to anonymize:
$folder = 'C:\Developer\psTest' # Enter the root path you want to monitor.
$filter = '*.htm' # You can enter a wildcard filter here.
$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
cmd.exe /c 'C:\Developer\psTest\runAnt.bat'
}
To be clear what I'm doing:
I have a file now called C:\Developer\psTest\FolderWatcherTest.ps1
that contains the code directly above.
When I right-click it and select Run with PowerShell, a console window flashes with some text but it's too fast to read before it closes.
When I then drag an .htm file into C:\Developer\psTest
, nothing happens.
I put the unregister code into a file called FolderWatcherStop.ps1, and when I click that, the console flashes with some red text, again too quick to read, then it closes.
What am I doing wrong? Something I'm sure.
UPDATE III
Following Dennis's advice I got this working. This took modifying the batch file a little to include the full path of the ANT build I want to run, but it works.
Note: I think I just got why the trigger is repeating, will update.