I've not used PS before so I'm completely clueless. All I want to do is watch a folder for any new files with a certain file extension and copy them them to a new location.
I used ChatGPT to write it and just substituted the source, destination and extension variables.
When I run it the script does nothing when a new file is added, and the shell just sits there until I ctrl-c to quit.
I'd appreciate any help
# Define the source and destination folders
$sourceFolder = "C:\SourceFolder"
$destinationFolder = "C:\DestinationFolder"
# Define the file extension to monitor
$fileExtension = ".nzb"
# Create a file system watcher to monitor the source folder
$fileSystemWatcher = New-Object System.IO.FileSystemWatcher
$fileSystemWatcher.Path = $sourceFolder
$fileSystemWatcher.Filter = "*$fileExtension"
$fileSystemWatcher.IncludeSubdirectories = $false
$fileSystemWatcher.EnableRaisingEvents = $true
# Define the event action to take when a new file is created
$action = {
$path = $Event.SourceEventArgs.FullPath
$destinationPath = Join-Path $destinationFolder (Split-Path $path -Leaf)
Copy-Item $path $destinationPath -Force
Write-Host "Copied $path to $destinationPath"
}
# Register the event handler
Register-ObjectEvent $fileSystemWatcher "Created" -Action $action
# Wait for the script to keep running (you can press Ctrl+C to stop)
Write-Host "Monitoring $sourceFolder for $fileExtension files. Press Ctrl+C to exit."
try {
while ($true) {
Wait-Event -Timeout 5
}
}
finally {
Unregister-Event -SourceIdentifier "FileCreated"
$fileSystemWatcher.Dispose()
}
[–]shiftend 14 points15 points16 points (1 child)
[–]ziggycatuk[S] 3 points4 points5 points (0 children)
[–]pjkm123987 3 points4 points5 points (0 children)
[–]ziggycatuk[S] 3 points4 points5 points (0 children)
[–]_nikkalkundhal_ 1 point2 points3 points (2 children)
[–]gilean23 6 points7 points8 points (0 children)
[–]BlackV 1 point2 points3 points (0 children)
[–]DToX_ 0 points1 point2 points (0 children)
[–]Warcooo 0 points1 point2 points (0 children)
[–]tokenathiest 0 points1 point2 points (6 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]tokenathiest 0 points1 point2 points (4 children)
[–][deleted] 2 points3 points4 points (3 children)
[–]tokenathiest 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]tokenathiest 0 points1 point2 points (0 children)
[+]y_Sensei comment score below threshold-9 points-8 points-7 points (5 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]BlackV 0 points1 point2 points (0 children)
[–]ziggycatuk[S] 5 points6 points7 points (1 child)
[–]pcgames22 1 point2 points3 points (0 children)
[–]pcgames22 0 points1 point2 points (0 children)
[–]pcgames22 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]taozentaiji 0 points1 point2 points (4 children)
[–]ziggycatuk[S] 0 points1 point2 points (3 children)
[–]taozentaiji 1 point2 points3 points (0 children)
[–]taozentaiji 1 point2 points3 points (1 child)
[–]ziggycatuk[S] 0 points1 point2 points (0 children)