The following powershell script copies all .docx files from $global:SRC to $global:DEST with a timestamp.
Save this file to something like "StartClone.ps1". Then launch it with "[right click] > Run with PowerShell." Minimize the resulting window.
### Configuration - CUSTOMIZE THESE $global:SRC = "C:\Users\fil\test" $global:DEST = "C:\Users\fil\backup" $global:LOGFILE = "C:\Users\fil\log.txt" ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $global:SRC $watcher.Filter = "*.docx" $watcher.IncludeSubdirectories = $true $watcher.EnableRaisingEvents = $true $rv=Test-Path "$global:DEST" if ($rv -eq $False) { New-Item -ItemType "directory" -Path "$global:DEST" } function global:logging ($text) { $logline = "$(Get-Date -uformat "%Y/%m/%d %H:%M:%S") - $text" Add-content $global:LOGFILE -value $logline } ### DEFINE ACTIONS AFTER AN EVENT IS DETECTED $action = { $path = $Event.SourceEventArgs.FullPath $changeType = $Event.SourceEventArgs.ChangeType global:logging "$changeType $path" $file = Split-Path $path -leaf $now = "$(Get-Date -uformat "%Y-%m-%d %H.%M.%S")" $dest = "$global:DEST\$now $file" # global:logging "file=$file now=$now dest=$dest" copy-item "$path" "$dest" } ### DECIDE WHICH EVENTS SHOULD BE WATCHED echo "Hello world $global:LOGFILE" Register-ObjectEvent $watcher "Created" -Action $action Register-ObjectEvent $watcher "Changed" -Action $action # Register-ObjectEvent $watcher "Deleted" -Action $action # Register-ObjectEvent $watcher "Renamed" -Action $action global:logging Started while ($true) {sleep 60}
No comments:
Post a Comment