Folder watcher with sub folders help?

Welcome Forums General PowerShell Q&A Folder watcher with sub folders help?

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

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #118464

    Participant
    Points: 15
    Rank: Member

    Hello, Total Powershell newbie here. This is my very first script.

    I have a script that monitors a folder, performs some replace functions and moves the file to another folder. This is all working well.

    The problem I have is if another folder with files in it is added to the watched folder, none of these actions are performed.

    I would like to get the files in sub directories, perform the same functions and move just the files themselves to the downloads\test folder and delete the sub directory.

    Also trying to change the filename to title case So The First Letter Of Every Word Is A Capital.

    This is what I have so far:

    ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Downloads\test"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true
    
    ### Create timer
    $timer = new-object timers.timer 
    $timer.Interval = 5000 #5 seconds
    $timer.Enabled = $true
    
    ### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $fileWatcherAction = {
    # Reset the timer every time the file watcher reports a change
    write-host "Timer Elapse Event: $(get-date -Format 'HH:mm:ss')"
    $timer.Stop()
    $timer.Start()
    }
    
    $timerAction = { $path = $Event.SourceEventArgs.FullPath
    $changeType = $Event.SourceEventArgs.ChangeType
    $logline = "$(Get-Date), $changeType, $path"
    Add-content "C:\Users\User\Desktop\log.txt" -value $logline
    
    Get-ChildItem C:\downloads\test\-Recurse | Foreach {Rename-Item -Path 
    $_.Fullname -newname "$($_.DirectoryName)\$($_.BaseName -replace'string 1','' 
    -replace'string 2','' -replace'string 3','')$($_.extension)"}
    
    Get-ChildItem C:\downloads\test\*.* | Foreach {Rename-Item -Path $_.Fullname 
    -newname "$($_.DirectoryName)\$($_.BaseName.trim())$($_.extension)"}
    
    Get-ChildItem C:\downloads\test\*.* | Move-Item -Destination 
    C:\downloads\test\temp
    }
    
    ### When timer fires timerAction is called
    Register-ObjectEvent $timer "Elapsed" -Action $timerAction
    
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED, every call of below events resets the timer
    Register-ObjectEvent $watcher "Created" -Action $fileWatcherAction
    while ($true) {Start-Sleep 5}

    Any help is greatly apreciated.

  • #118488

    Participant
    Points: 268
    Helping Hand
    Rank: Contributor

    change words to title case:

    $words = 'some random words to show the effect'
    $TextInfo = (Get-Culture).TextInfo
    $TextInfo.ToTitleCase($words)
  • #118491

    Participant
    Points: 15
    Rank: Member

    Thank you very much Olaf that works great.

    Still need to work out how to process files in sub dirs and move the files into the downloads\test\temp folder and delete the original folders the moved files were in.

You must be logged in to reply to this topic.