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!
&{ $Var = 1; &{ $Var = 2; $Var }; $Var}
, so setting variable in inner block not necessary change that variable in outer block. – user4003407$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. – sloppyfrenzyIf ($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