Skip to content

Instantly share code, notes, and snippets.

@dtzitz
Created September 20, 2018 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtzitz/e9f53f499f57abf12f414eb721befc3f to your computer and use it in GitHub Desktop.
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
#
# 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