- Oct 16, 2020
- 60
- 957
I don’t know if this is the right tread but it’s on etsy.
You must be registered for see links
You must be registered for see links
# Made by QwertyYou must be registered for see links
# Inspired byYou must be registered for see links
# Run this script by '.\watch.ps1' to start watching current folder.
# Stop by pressing Ctrl+C
try {
$watcher = New-Object IO.FileSystemWatcher -Property @{
Path = '.'
Filter = '*.*'
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$action = {
$details = $Event.SourceEventArgs
$Path = $details.FullPath
$Name = $details.Name
$OldName = $details.OldName
$filename = [io.path]::GetFileNameWithoutExtension($Name)
$ext = [io.path]::GetExtension($Name)
if ( $OldName -eq $null ) {
$count = 0
do {
$count = $count + 1
$newName = "$filename-$count$ext"
} while ( test-path $newName )
Move-Item $Path -Destination $newName -Force
Write-Host ""
Write-Host "$Name -> $newName" -ForegroundColor DarkYellow
}
}
$handlers = . { # available event types: Created Deleted Changed Renamed
Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action
}
# Monitoring starts now:
$watcher.EnableRaisingEvents = $true
Write-Host "Watching for changes. Press Ctrl+C to stop."
# Use an endless loop to keep PowerShell busy.
do {
Wait-Event -Timeout 1
Write-Host "." -NoNewline # write a dot to indicate we are still monitoring
} while ($true)
} finally {
# This gets executed when user presses CTRL+C:
$watcher.EnableRaisingEvents = $false
$handlers | ForEach-Object { Unregister-Event -SourceIdentifier $_.Name }
$handlers | Remove-Job
$watcher.Dispose()
Write-Host ""
Write-Host "Event Handler disabled, watching ends."
}