0

[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:

PowerShell Properties

Fat Monk
  • 2,077
  • 1
  • 26
  • 59
  • 3
    You are inside of a scriptblock when `$destinationFile` is created and referencing `$destinationFolder` from outside of the script block. The script block scope knows nothing about that variable. – AdminOfThings Jul 31 '19 at 16:22
  • Unless you're on XP, I highly doubt you're on powershell 1.0 – Maximilian Burszley Jul 31 '19 at 16:28
  • Looks like I need to read up on variable scope in Powers hell in that case. – Fat Monk Jul 31 '19 at 17:04
  • @TheIncorrigible1 I've uploaded a screenshot of why I think/thought this is/was PowerShell v.10 - see the version number in the path. This could, of course, be misleading me, but that is what I based the assumption on. – Fat Monk Aug 20 '19 at 09:53

0 Answers0