[Note, this is a repost of a question that failed to demonstrate the problem as it only used snippets of my actual code. This is the actual code and this definitely fails on my system]
I'm trying to add a filename to the end of a predefined path in PowerShell v1.0 but the path is not being pre-pended.
Set-PSDebug -Trace 2
## Configure here
$sourceFolder = "C:\Users\myusername\Desktop\Folder 1"
$fileFilter = "*.ext"
$destinationFolder = "C:\Folder 2"
$logFile = "C:\Users\myusername\Desktop\ofp_dupe_log.txt"
### Add an entry to log file to say started script
$logline = "`n`n############################################################`n$(Get-Date), ext_dupe script started`nWatching: $sourceFolder`nDestination 1: $destinationFolder1`nDestination 2: $destinationFolder2`n############################################################"
Add-content $logFile -value $logline
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $sourceFolder
$watcher.Filter = $fileFilter
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$source_file = Split-Path $path -leaf
$destinationFile = $destinationFolder+"\"+$source_file
$logline = "$(Get-Date), $changeType, $path`ndest1: $destinationFile"
Add-content "C:\Users\myusername\Desktop\ofp_dupe_log.txt" -value $logline
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
Register-ObjectEvent $watcher "Changed" -Action $action
Register-ObjectEvent $watcher "Deleted" -Action $action
Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}
I can't see what is wrong, but my logFile.txt only shows:
############################################################
07/31/2019 17:09:47, ext_dupe script started
Watching: C:\Users\myusername\Desktop\Folder 1
Destination 1:
Destination 2:
############################################################
07/31/2019 17:09:52, Changed, C:\Users\myusername\Desktop\Folder 1\match1.ext
dest1: \match1.ext
07/31/2019 17:09:52, Changed, C:\Users\myusername\Desktop\Folder 1\match1.ext
dest1: \match1.ext
i.e. The $destinationFolder string is not be prefixed before the file name.
NOTE: To people questioning whether I am using PowerShell v1.0 I am basing this claim on the fact that the properties window of the PS Shell shows v.10
in the path. This is running nuder Win10. See below: