Created
June 13, 2018 02:31
-
-
Save ravenstar/6ab143581d20fd569c1c08a6858c68cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Watches for changes to files matching the $watchFilter within the $watchDir | |
# directory, then uploads them to a FTP server. | |
# It is recommended to use a scheduled task to ensure this script runs on | |
# system boot. | |
# | |
## Config - Change these. | |
$ftpUrl = "ftp://username:password@example.com/some/folder/" | |
$watchDir = "C:\temp\" | |
$watchFilter = "*.txt" | |
## Do Not Edit Below | |
function uploadFile($fullFileName) | |
{ | |
$webclient = New-Object System.Net.WebClient | |
$fileName = [system.io.path]::GetFileName($fullFileName) | |
$fileUrl = $ftpUrl+$fileName | |
$uri = New-Object System.Uri($fileUrl) | |
try | |
{ | |
$rc = $webclient.UploadFile($uri, $fullFileName) | |
} | |
catch [System.Net.WebException] | |
{ | |
Write-Host "[ERR]: "$_ | |
return | |
} | |
Write-Host "Uploaded $fullFileName" | |
} | |
$watcher = New-object System.IO.FileSystemWatcher $watchDir | |
$watcher.EnableRaisingEvents = $true | |
$watcher.Filter = $watchFilter | |
$changed = Register-ObjectEvent $watcher "Changed" -Action { | |
Write-Host $eventArgs.ChangeType, $eventArgs.Fullpath | |
uploadFile $eventArgs.Fullpath | |
} | |
while($true) { | |
echo "." | |
start-sleep -s 10 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment