locked
FileSystemWatcher only runs from ISE RRS feed

  • General discussion

  • Hi,

    I found a FileSystemWatcher script online to monitor a folder for new files.  The script only runs in ISE.  When I run the PS1 file, nothing happens.

    I called the PS1 file from a batch file.  Here is the code:

    powershell.exe -ExecutionPolicy Bypass -File D:\Scripts\Log\scripts\EnableDirMon.ps1

    Here is the code from the PS1 file:

    $extensions = Get-Content "d:\scripts\log\videoextensions.txt"
    $downloaddir = Get-Content "d:\scripts\log\downloaddir.txt"

    foreach($ext in $extensions){

    $extname = "*." + $ext

    $folder = $downloaddir
    $filter = $extname
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
     IncludeSubdirectories = $true
     NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    }
    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier $ext -Action {
     #$path = $Event.SourceEventArgs.FullPath
     $name = $Event.SourceEventArgs.Name

    $check = $name | Select-String -Pattern "\\"

    if(-not ([string]::IsNullOrEmpty($check)) -eq "True"){

    $split = $name -split "\\"
    $split = $split[1]

    $split | Add-Content "d:\scripts\log\videolog.txt"

    }else{

    $name | Add-Content "d:\scripts\log\videolog.txt"

    }
    }
    }

    Any suggestions on how to get the script to run would be appreciated.

    • Changed type Bill_Stewart Thursday, January 25, 2018 10:18 PM
    • Moved by Bill_Stewart Thursday, January 25, 2018 10:18 PM This is not "fix/debug/rewrite my script for me" forum
    Saturday, November 11, 2017 2:08 AM

All replies

  • Add this at the end of your script.

    while(1){sleep 1}


    \_(ツ)_/

    Saturday, November 11, 2017 2:14 AM
  • What does "when I run the PS1 file" mean? You must be detailed and specific in your question. (Remember: nobody can see your screen.)


    -- Bill Stewart [Bill_Stewart]

    Saturday, November 11, 2017 4:08 PM
  • I put while(1){sleep 1} at the end of my script.  When I run the batch file that executes the PS1 file, the batch file just stays open and nothing else happens.
    Sunday, November 12, 2017 9:44 PM
  • What I mean by run the PS1 file is that I execute the PS1 file from a batch file.  I put the following code in a batch file:

    powershell.exe -ExecutionPolicy Bypass -File D:\Scripts\Log\scripts\EnableDirMon.ps1

    I save the batch file and then double-click the file to execute the PS1 file.

    Sunday, November 12, 2017 9:46 PM
  • Why would you use a batch file?

    Yes.  The script has to remain running for the FSW to work.


    \_(ツ)_/

    Sunday, November 12, 2017 9:49 PM
  • I found a FileSystemWatcher script online to monitor a folder for new files. The script only runs in PowerShell ISE. When I run the PS1 file, nothing happens.

    I called the PS1 file from a batch file. Here is the code:

    powershell.exe -ExecutionPolicy Bypass -File D:\Scripts\Log\scripts\EnableDirMon.ps1

    Here is the code from the PS1 file:

    $extensions = Get-Content "d:\scripts\log\videoextensions.txt"
    
    $downloaddir = Get-Content "d:\scripts\log\downloaddir.txt"
    
    foreach($ext in $extensions){
    
      $extname = "*." + $ext
    
      $folder = $downloaddir
      $filter = $extname
      $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
             IncludeSubdirectories = $true
             NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
      }
      $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier $ext -Action {
         #$path = $Event.SourceEventArgs.FullPath
         $name = $Event.SourceEventArgs.Name
         $check = $name | Select-String -Pattern "\\"
    
         if(-not ([string]::IsNullOrEmpty($check)) -eq "True"){
             $split = $name -split "\\"
             $split = $split[1]
             $split | Add-Content "d:\scripts\log\videolog.txt"
         } else {
             $name | Add-Content "d:\scripts\log\videolog.txt"
         }
       }
    }

    Any suggestions on how to get the script to run would be appreciated.

    • Merged by jrv Sunday, November 12, 2017 10:00 PM duplicate
    Sunday, November 12, 2017 9:52 PM
  • Please do not post the same question multiple times.  I have merged your two posts.

    The answer is the same for both.  You cannot allow the script to end.  In ISE the script keeps running because ISE is still running.

    In CLI start the script at a prompt and it will work.

    If the script has no output you can start it from a shortcut with "-WindowStyle hidden" and it will not be visible.


    \_(ツ)_/

    Sunday, November 12, 2017 10:03 PM
  • I wasn't aware that the script had to stay running.  Thanks for your input! After adding while(1){sleep 1} to my script, everything works as expected.  To answer your question, I put the PS1 in a batch file and called the batch file from a userform in Excel.  At the time I created the spreadsheet I didn't realize that I can call the PS1 directly.  I have a number of batch files in place in the spreadsheet and did not get around to changing the code.  Thanks again for your help!
    Monday, November 13, 2017 2:23 AM