question

Miguel-PSNewbie123 avatar image
0 Votes"
Miguel-PSNewbie123 asked Miguel-PSNewbie123 answered

Create folders from a Textbox - Windows Form

Hello Everyone,
I am hoping you can help me with the following error. I am trying to create folders name in each Line of the TextBox.
The Windows Form opens, and the Button works, but I am not sure why it is not creating folders in the specific location.
If the textbox contains:
Miguel1
Miguel2
Miguel3

I would like 3 folders be created with those names.
I hope you can help me
Thank you


 # Load required assemblies
 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    
 # Drawing form and controls
 $CreateFolder = New-Object System.Windows.Forms.Form
     $CreateFolder.Text = "Create Folder"
     $CreateFolder.Size = New-Object System.Drawing.Size(800,400)
     $CreateFolder.FormBorderStyle = "FixedDialog"
     $CreateFolder.TopMost = $true
     $CreateFolder.MaximizeBox = $false
     $CreateFolder.MinimizeBox = $false
     $CreateFolder.ControlBox = $true
     $CreateFolder.StartPosition = "CenterScreen"
     $CreateFolder.Font = "Segoe UI"
    
 # Inputbox    
     $Inputbox = New-Object System.Windows.Forms.TextBox
     $Inputbox.Multiline = $True;
     $Inputbox.Location = New-Object System.Drawing.Size(20,50)
     $Inputbox.Size = New-Object System.Drawing.Size(300,200)
     $Inputbox.ScrollBars = "Vertical"
     $CreateFolder.Controls.Add($Inputbox)
     $Inputbox = $Folders
        
 # adding a label to my form
 $label_message = New-Object System.Windows.Forms.Label
     $label_message.Location = New-Object System.Drawing.Size(8,8)
     $label_message.Size = New-Object System.Drawing.Size(240,32)
     $label_message.TextAlign = "MiddleCenter"
     $label_message.Text = "Create Folders!!"
     $CreateFolder.Controls.Add($label_message)
    
 Set-Location "c:\Users\miguel\Desktop"
 New-Item eMail -type directory
 Set-Location "c:\Users\miguel\Desktop\eMail"
    
 # add a button
 $button_ClickMe = New-Object System.Windows.Forms.Button
     $button_ClickMe.Location = New-Object System.Drawing.Size(350,80)
     $button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
     $button_ClickMe.TextAlign = "MiddleCenter"
     $button_ClickMe.Text = "Create Folders Now!!!"
     $button_ClickMe.Add_Click({
         $button_ClickMe.Text = "Folders were created"
         ForEach ($Folder in $Folders) {
             New-Item $Folder.Name -type directory
             }
     })
     $CreateFolder.Controls.Add($button_ClickMe)
    
    
 # show form
 $CreateFolder.Add_Shown({$CreateFolder.Activate()})
 [void] $CreateFolder.ShowDialog()
windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

Maybe you should use another loop: ForEach ($Folder in $Inputbox.Lines)?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Miguel-PSNewbie123 avatar image
0 Votes"
Miguel-PSNewbie123 answered

That help!
Thank you

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.