我正在尝试创建Powershell脚本,该脚本将搜索指定的文件夹并将所有子文件夹列出到列表框中。当用户从列表框中选择文件夹时,将执行操作。
我对Powershell还是很陌生,所以我不知道我是否走在正确的轨道上,但是我尝试将目录放入数组中,我只是不知道如何将数组放入列表框中。
$items = Get-ChildItem -Path $path
foreach ($item in $items)
{
# if the item is a directory, then process it.
if ($item.Attributes -eq "Directory")
{
Write-Host $item.Name
}
}
答案 0 :(得分:1)
对于演示,这是一个简单的表单,其中用子文件夹路径填充列表框。
$rootFolder = 'PATH TO YOUR ROOTFOLDER HERE'
# get an array of subfolder full names in the $rootFolder
$subfolders = (Get-ChildItem -Path $rootFolder -Recurse -Directory).FullName
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = "SubFolders"
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = "CenterScreen"
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,180)
$listBox.Anchor = 'Top,Right,Bottom,Left'
# fill the listbox with subfolder names
$listBox.items.AddRange($subfolders)
# add an event handler on the listbox to do something with the selected item
$listBox.Add_Click({
# here put your code to perform some action with the selected subfolder
$selected = $listBox.GetItemText($listBox.SelectedItem)
# for demo, simply show a messagebox
[System.Windows.Forms.MessageBox]::Show("You selected subfolder`r`n`r`n$selected", "Subfolder")
})
$form.Controls.Add($listBox)
$form.ShowDialog()
$form.Dispose()
根据您的评论,您想在显示LastWriteTime(lastModified)日期的列表框中添加第二列。
可以很容易地改编早期代码,以添加字符串项,这些字符串项是文件夹FullNames的组合,并使用某种分隔字符将其与LastWriteTime组合在一起。稍后,将所选字符拆分到该字符上,以仅获取文件夹名称。
但是,就用户体验而言,这会使整个事情变得一团糟。.
添加新列会更好,但是尽管Listbox对象确实具有MultiColumn
属性,但使用它的结果很可能不您期望的结果。
在下面的更新的代码中使用了ListView对象而不是ListBox
对象中的多个列。
$rootFolder = 'PATH TO YOUR ROOTFOLDER HERE'
# get an array of subfolder objects in the $rootFolder
$subfolders = Get-ChildItem -Path $rootFolder -Recurse -Directory
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = "SubFolders"
$form.Size = New-Object System.Drawing.Size(600,400)
$form.StartPosition = "CenterScreen"
$listView = New-Object System.Windows.Forms.ListView
$listView.Location = New-Object System.Drawing.Point(10,40)
$listView.Size = New-Object System.Drawing.Size(560,280)
$listView.Anchor = 'Top,Right,Bottom,Left'
$listView.View = 'Details'
$listView.FullRowSelect = $true
$listView.GridLines = $true
[void]$listView.Columns.Add("Folder", 338);
[void]$listView.Columns.Add("LastModified", 200);
# fill the listbox with subfolder names and Last Modified dates
$subfolders | ForEach-Object {
$row = New-Object System.Windows.Forms.ListViewItem( $_.FullName) # the folder path goes into the first column
[void]$row.SubItems.Add($_.LastWriteTime.Tostring()) # the LastWriteTime goes into the second column
[void]$listView.Items.Add($row)
}
# add an event handler on the listbox to do something with the selected item
$listView.Add_Click({
# here put your code to perform some action with the selected subfolder
$selected = $listView.SelectedItems[0].Text
# for demo, simply show a messagebox
[System.Windows.Forms.MessageBox]::Show("You selected subfolder`r`n`r`n$selected", "Subfolder")
})
$form.Controls.Add($listView)
[void]$form.ShowDialog()
$form.Dispose()
希望有帮助