Forum Discussion
pspBugs
Sep 21, 2023Copper Contributor
Folder select and paste in a textbox
Hi all, i need to change the timestamp of files and folders and i got a form running where i can set the path of the destination. But i want to choose the folder with the selector and then write it in the textbox.
The selection dialog is also running but i can't change the textbox.text to my variable. So i changed the script to take it directly from the textbox.
Clear-Host
$scriptlocation = $(Split-Path $Script:MyInvocation.MyCommand.Path)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Discribing the form.
$ItemChangeApplikation = New-Object system.Windows.Forms.Form
$ItemChangeApplikation.ClientSize = New-Object System.Drawing.Point(800,300)
$ItemChangeApplikation.text = "ItemChangeApplikation"
$ItemChangeApplikation.TopMost = $false
$ItemChangeApplikation.icon = "$scriptlocation\logo.ico"
$ItemChangeApplikationImage = [system.drawing.image]::FromFile("$scriptlocation\backgpic.png")
$ItemChangeApplikation.BackgroundImage = $ItemChangeApplikationImage
$ItemChangeApplikation.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
#Getting the Label for the discribtion.
$ExplainLabel = New-Object system.Windows.Forms.Label
$ExplainLabel.text = "This script can be used to tag the files and folders with today's date as the modification date.
To do this, simply select the folder that contains the folders that are to be protected from deletion by not using / changing them by clicking on the SELECT button.
Alternatively, the text box can also be filled with the corresponding path at this point. To change the time of the last change, simply click OK or cancel the process by clicking Cancel."
$ExplainLabel.AutoSize = $false
$ExplainLabel.width = 750
$ExplainLabel.height = 150
$ExplainLabel.location = New-Object System.Drawing.Point(25,50)
$ExplainLabel.Font = New-Object System.Drawing.Font('Arial',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$ExplainLabel.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$ItemChangeApplikation.controls.Add($ExplainLabel)
$ItemPath = New-Object system.Windows.Forms.TextBox
$ItemPath.multiline = $false
$ItemPath.text = "Folderpath"
$ItemPath.width = 750
$ItemPath.height = 30
$ItemPath.location = New-Object System.Drawing.Point(25,220)
$ItemPath.Font = New-Object System.Drawing.Font('Arial',18)
$ItemPath.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$ItemPath.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$ItemChangeApplikation.controls.Add($ItemPath)
#declaration for the buttons.
$Cancel = New-Object system.Windows.Forms.Button
$Cancel.text = "Cancel"
$Cancel.width = 99
$Cancel.height = 30
$Cancel.location = New-Object System.Drawing.Point(676,10)
$Cancel.Font = New-Object System.Drawing.Font('Arial',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$Cancel.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$ItemChangeApplikation.controls.Add($Cancel)
$Ok = New-Object system.Windows.Forms.Button
$Ok.text = "Ok"
$Ok.width = 99
$Ok.height = 30
$Ok.location = New-Object System.Drawing.Point(25,10)
$Ok.Font = New-Object System.Drawing.Font('Arial',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$Ok.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$ItemChangeApplikation.controls.Add($Ok)
$Selector = New-Object system.Windows.Forms.Button
$Selector.text = "Selector"
$Selector.width = 99
$Selector.height = 30
$Selector.location = New-Object System.Drawing.Point(375,10)
$Selector.Font = New-Object System.Drawing.Font('Arial',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$Selector.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$ItemChangeApplikation.controls.Add($Selector)
# code for the button
# close the window
$Cancel.Add_Click({$ItemChangeApplikation.close() })
$Selector.add_click({
# load Assembly for the form
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
# Select the folder dialog
$PathSelect = New-Object System.Windows.Forms.FolderBrowserDialog
$PathSelect.Tag ='needed things ...'
$PathSelect.RootFolder="MyComputer"
$PathSelect.Description = "Choose the folder."
# No way to create a new folder in here
$PathSelect.ShowNewFolderButton = $false
# dialog show
$PathSelect.ShowDialog()
# if a folder was choosen (do not abort>> "-ne" for no exit)
if ($PathSelect.SelectedPath -ne "")
{
$SelectedItem = $PathSelect.SelectedPath
$ItemPath.Clear()
$ItemPath.Paste($SelectedItem)
}
})
$Ok.add_click({
$SelectedItem = $ItemPath.text
ChangeTime
})
[void]$ItemChangeApplikation.ShowDialog()
function ChangeTime{
Get-ChildItem $ItemPath.text -Recurse | % {$_.LastWriteTime = (Get-Date)}
}
Thanks for the help.
PS: I found the solution which is working for me. I simply had to add $ItemPath.Clear() and $ItemPath.Paste($SelectedItem) inside the if syntax instead trying change $ItemPath.Text directly.
No RepliesBe the first to reply