0
votes

I see this question has been asked a lot, but nothing I've seen explains why I'm having this issue... If anyone can bring me back from the brink of insanity I'd much appreciate it.

Here's a code sample.

$Browse_IE_Click = { $Browse_Identifier = "1"; & $Browse_Click }

$Browse_Click = { 

[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$File_Browse = New-Object System.Windows.Forms.OpenFileDialog
[void]$File_Browse.ShowDialog()

$fullpath = $File_Browse.FileName
$file = Split-Path -Path $File_Browse.FileName -Leaf

If ($Browse_Identifier -eq "1") { $Flash_IE_New.Text = "New: " + "$($file)"; $Flash_IE_File = $file }

} # End Browse_Click

$Save_Click = {

If ($Flash_IE_New.Text -ne "New: ") { 

    Remove-Item -Path "$dir\Files\Installs\$Flash_IE_Current_File"
    Copy-Item $fullpath "$dir\Files\Installs\$Flash_IE_File"

} # End If

} # End Save_Click

There's a GUI that goes with this with multiple browse buttons. Each browse button goes to its own $Browse_Something_Click scriptblock which gives it an identifying number then goes to $Browse_Click. That's my way of saving space and only have one real Browse scriptblock.

Anyway, the whole chosen file and path is put into $fullpath and it's also split into just the file name which is $file. Depending on which browse button is selected it saves the "file" into a more specific variable, in this case $Flash_IE_File.

When the "Save" button is clicked if something was chosen it removes the current file in the folder and copies the newly selected file into that same folder.

It all works fine except the Copy-Item line. I've tried adding -Path and -Destination, also tried putting $fullpath in quotes and\or $($fullpath). I've tried moving $fullpath = $File_Browse.FileName into different places in the If ($Browse_Identifier -eq "1") statement. I've tried splitting it up so instead of saving the whole path I just save the parent and then for the Copy path I use $parent\$file. At that point I got a new error about the path not being a drive letter or UNC path. Nothing works.

I've tried running a test through the Powershell console and it works fine. I browse, save to a variable, copy that variable to a different location, no issues.

Help!

1
Are you sure you've got the right data in the relevant variables that you're expecting? It could be the case that '$File_Browse.FileName' doesn't contain exactly what you're hoping for an therefor by the time you get to actually copying an item is genuinely is null. Try adding a textbox to your gui and populating it with your variable's content on button click as a quick double check.Windos
Variables are scoped: &{ $Var = 1; &{ $Var = 2; $Var }; $Var}, so setting variable in inner block not necessary change that variable in outer block.user4003407
Thanks for the reply, Windos. During the powershell console test it contained exactly what I thought it should. Also, the $Flash_IE_New.Text = "New: " + "$($file)" section is a label on the GUI that correctly changes with the selected file name. I'm not sure what's changing between that point and the Copy-Item point. I'd think the value should be correct because it's saving the whole path to the $fullpath variable before splitting it to get the file name (which displays in the GUI fine). It works for $file variable, why wouldn't it work for $fullpath? That's where I'm lost.sloppyfrenzy
Thanks, PetSerAl. The rest of the script that this is a part of has the same variables used in multiple scriptblocks and functions and the values carry over without a problem. This is the first time I've run into this.sloppyfrenzy
So I just tried something - I moved the Copy-Item line into the end of the If ($Browse_Identifier -eq "1") statement and it copied it over, no problem. So it's a scoping issue? Any suggestions to get around that?sloppyfrenzy

1 Answers

0
votes

Credit goes to PetSerAl. I just remembered the $script:fullpath option and that seemed to take care of the scoping issue.

$script:fullpath = $File_Browse.FileName
$file = Split-Path -Path $File_Browse.FileName -Leaf

If ($Browse_Identifier -eq "1") { $Flash_IE_New.Text = "New: " + "$($file)"; $Flash_IE_File = $file }

$Save_Click = {

If ($Flash_IE_New.Text -ne "New: ") { 

    Remove-Item -Path "$dir\Files\Installs\$Flash_IE_Current_File"
    Copy-Item $fullpath "$dir\Files\Installs\$Flash_IE_File"

} # End If

} # End Save_Click

Thanks!