3

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.

Community
  • 1
  • 1
user3762977
  • 651
  • 2
  • 15
  • 27

1 Answers1

2

Here you go:

$folder = 'f:\test' # Enter the root path you want to monitor. 
$filter = '*.html'  # 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 
write-host "test"
Invoke-Item 'f:\test\test.bat'
}
Dennis
  • 36
  • 3
  • You can stop the event with: Unregister-Event FileCreated – Dennis Sep 04 '15 at 20:21
  • @user3762977.. hmm what does your batch file do? Seems like there is a problem with that.. – Dennis Sep 04 '15 at 21:10
  • Still nothing. The batch file runs an ANT build.xml; it works fine if I double-click the `runAnt.bat` file directly. It's conceivable that my system won't allow this, wouldn't be the first thing that was restricted. I have read/write access to these folders, but I don't have administrator access to the computer. – user3762977 Sep 04 '15 at 21:35
  • There must be a way to see the messages being returned by Powershell, is there? The console just flashes them and then closes, way too quick to read. – user3762977 Sep 04 '15 at 21:36
  • @user3762977, do you have access to Powershell ISE (script editor)? If so, open your .ps1 file there and run it. – Dennis Sep 04 '15 at 21:41
  • That did it. Found a syntax error (mine, an extra `'`) and when I fixed that the .bat file triggered as wanted. Thanks for the lesson, great. – user3762977 Sep 04 '15 at 21:50
  • One more question Dennis if you're still watching, thanks again. – user3762977 Sep 05 '15 at 00:19
  • Never mind, the batch file was being triggered over and over but I just realized it's because the ANT build is transforming the .htm file and saving repeated times, d'oh. Can easily fix that. – user3762977 Sep 05 '15 at 00:31