Created
September 20, 2018 02:37
-
-
Save dtzitz/e9f53f499f57abf12f414eb721befc3f to your computer and use it in GitHub Desktop.
powershell script to watch a directory and copy any file that shows up in it
This file contains hidden or 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
# | |
# Script.ps1 | |
# | |
$folder = 'C:\dev\copy' # <-- change this to the directory that will be watched | |
$filter = '*.*' # <-- set this according to your requirements | |
$destination = 'C:\dev\paste' # <-- change this to the directory that will hold a copy of the file(s) | |
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ | |
IncludeSubdirectories = $false # <-- set this according to your requirements | |
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite' | |
} | |
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { | |
$path = $Event.SourceEventArgs.FullPath | |
$name = $Event.SourceEventArgs.Name | |
$changeType = $Event.SourceEventArgs.ChangeType | |
$timeStamp = $Event.TimeGenerated | |
Write-Host "The file '$name' was $changeType at $timeStamp" | |
Copy-Item $path -Destination $destination -Force -Verbose # Force will overwrite files with same name | |
} | |
# Eventually unregister the script with the below commented line: | |
# Unregister-Event -SourceIdentifier FileCreated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment