Folder monitoring

Welcome Forums General PowerShell Q&A Folder monitoring

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #105049

    Participant
    Points: 0
    Rank: Member

    Hi all,

    here i go again, one more time I'm trying to get into Powershell programming. I have a project between hands where programming is involved. Since i'm a sysadmin and not a programmer, i'm struggling with this, but I don't wanna give up. I've tried many times to learn PS from scratch but then i'm not using it in my daily tasks and it's always fading away.
    Now I wanna use this project to push forward and stablish my knoledge. I'm adapting other scripts for my purpose.
    I'd really appreciate if someone can help me with that.
    I'm building an app were i'm controlling Photo cameras over USB relays, then the cameras copy the Jpgs in a Temp folder, from there they should be moved to another folder.
    With FileSystemWatcher i wanna know when the last picture is transferred to the temp folder in order to continue with process. I put this lines together, but is not working.

    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "c:\temp"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true
    
    $nrfiles = 0
    
    Register-ObjectEvent $watcher "Created" -Action {$nrfiles++}
    
    
    do {
         Write-Host "." -NoNewline
        } until ($nrfiles -eq "3")

    The value of $nrfiles doesn't increase 🙁

    For any help, thanx in advance.

  • #105052

    Participant
    Points: 13
    Rank: Member

    try changing

    Register-ObjectEvent $watcher "Created" -Action {$nrfiles++}

    to

    Register-ObjectEvent -inputObject $watcher -eventname Created -Action {$nrfiles++}

    • #105053

      Participant
      Points: 0
      Rank: Member

      Hi Simon B,

      still doesn't increase. No idea where can be the problem.

      Thanks anyway for the quick reply.

      Greetings

  • #105058

    Participant
    Points: 13
    Rank: Member

    Not sure if this will help you but this will move created txt files from one directory to another

    $Source = 'C:\Temp'
    $Filter = '*.txt'                             # < -- set this according to your requirements
    $destination = 'c:\Test'
    $Watcher= New-Object IO.FileSystemWatcher $Source, $filter -Property @{
     IncludeSubdirectories = $true              # <-- set this according to your requirements
     NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    }
    
    $onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
    $Path = $Event.SourceEventArgs.FullPath
    Move-Item $Path -Destination $destination -verbose
    
    }
    • #105202

      Participant
      Points: 0
      Rank: Member

      Hi Simon B,

      thank you very much for the script!, i was able to adapt it to my purpose.

      Greetings

The topic ‘Folder monitoring’ is closed to new replies.