i'm creating a gui in powershell for copying files. first i add a file with a button, next i choose the folder where to copy and then i want to copy. unfortunately the script says the file path is empty. how can i solve this problem? Furthermore, i want to add 2 functions.
a warning if no checkbox is marked
a text field next to the choose button where i can see the path from the file i want to copy
$PSDefaultParameterValues['*:Encoding'] = 'ascii' Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing #create form $form = New-Object System.Windows.Forms.Form $form.Width = 500 $form.Height = 300 $form.MaximizeBox = $false #choose file button $Button = New-Object System.Windows.Forms.Button $Button.Location = New-Object System.Drawing.Size(10,10) $Button.Size = New-Object System.Drawing.Size(150,50) $Button.Text = "choose file" $Button.Add_Click({ Function Get-FileName($initialDirectory) { [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = “All files (*.*)| *.*” $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename } $file = Get-FileName -initialDirectory “c:” }) $form.Controls.Add($Button) #create checkbox1 $checkBox = New-Object System.Windows.Forms.CheckBox $checkBox.Location = New-Object System.Drawing.Point (10, 100) $checkBox.Size = New-Object System.Drawing.Size(350,30) $checkBox.Text = "folder 1" $form.Controls.Add($checkBox) #create checkbox2 $checkBox2 = New-Object System.Windows.Forms.CheckBox $checkBox2.Location = New-Object System.Drawing.Point (10, 150) $checkBox2.Size = New-Object System.Drawing.Size(350,30) $checkBox2.Text = "folder 2" $form.Controls.Add($checkBox2) #copy file button $Button2 = New-Object System.Windows.Forms.Button $Button2.Location = New-Object System.Drawing.Size(10,200) $Button2.Size = New-Object System.Drawing.Size(150,50) $Button2.Text = "copy file" $Button2.Add_Click({ #checkbox1 action if ($checkBox.Checked -eq $true) { copy-item -Path $file -Destination "C:\folder 1" } #checkbox2 action if ($checkBox2.Checked -eq $true) { copy-item -Path $file -Destination "C:\folder 2" } }) $form.Controls.Add($Button2) #end [void]$form.ShowDialog()
CodePudding user response:
There are plenty of improvements to be suggested but let's focus on your questions:
The problem is that the scope of your $file variable is local to the function Get-Filename. So when accessed from outside of the function, it is always null.
As stated in the comments, the minimum modification for your script to work is the change the following line (I would also suggest to declare it initially outside of the function, at global level for sake of clarity):
# From this
$file = Get-FileName -initialDirectory "c:"
# To this
$Global:file = Get-FileName -initialDirectory "c:"
To add a textbox with the choosen filename, proceed as for the other controls:
#Text box with choosen file name
$txtBox = New-Object System.Windows.Forms.TextBox
$txtBox.Location = New-Object System.Drawing.Point (180, 20)
$txtBox.Size = New-Object System.Drawing.Size(280,20)
$form.Controls.Add($txtBox)
And to update the text, add the following line after setting the $Global:file variable:
$Global:file = Get-FileName -initialDirectory "c:"
$txtBox.Text = $Global:file
Finally, a message can be displayed if you add the following code to your $Button2.Add_Click code:
#nothing checked
if(($checkBox.Checked -eq $false) -and ($checkBox.Checked -eq $false)) {
[System.Windows.Forms.Messagebox]::Show("No CheckBox checked")
}
