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.