gpt4 book ai didi

powershell - 使用 PSCustomObject 在单独的列中列出多个文件夹然后选择一些?

转载 作者:行者123 更新时间:2023-12-04 13:07:27 26 4
gpt4 key购买 nike

早上好

我的桌面上有 3 个文件夹,每个文件夹包含随机数量的文件夹。

  • 文件夹 1

    • 子文件夹:“here too”、“more stuff”、“Ranom stuff”、“Something Here”
  • 文件夹 2

    • 子文件夹:“和这里”、“无聊”、“这里的东西”、“这里也是”、“这里的东西”
  • 文件夹 3

    • 子文件夹:“Hello”、“Howdy”、“More Stuff here”、“Random”、“Random here”、“Space”

我要完成的任务:根据主文件夹列出每个单独列中的子文件夹,例如:

SelectionOne      SelectionTwo  SelectionThree    
------------ ------------ --------------
0: here too 4: and here 9: Hello
1: more stuff 5: bored 10: Howdy
2: Ranom stuff 6: Here stuff 11: More Stuff here
3: Something Here 7: here too 12: Random
8: stuff here 13: Random here
14: Space

Enter Folder Numbers: 3, 14

然后可以通过选择的编号选择对应的文件夹如:

# from the 3, and 14 selection it would be the following selected:
'Something Here'
'Space'

到目前为止,我可以在单独的列中列出文件夹,只是不分配前一列的增量值。我面临的另一个问题是尝试为该数组选择正确的索引号并以某种方式将其关联到正确的文件夹?即,当我选择 3,14 时,第一列和第三列中的相应文件夹被选中,但它也只显示为:

SelectionOne      SelectionTwo  SelectionThree    
------------ ------------ --------------
0: here too 0: and here 0: Hello
1: more stuff 1: bored 1: Howdy
2: Ranom stuff 2: Here stuff 2: More Stuff here
3: Something Here 3: here too 3: Random
4: stuff here 4: Random here
5: Space

这是我所拥有的:

$Folder1 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 1'
$Folders1 = for ($i=0; $i -lt $Folder1.Count; $i++) {
[PSCustomObject]@{
FolderNames1 = "${i}: $($Folder1[$i].Name)"
}
}


$Folder2 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 2'
$Folders2 = for ($i=0; $i -lt $Folder2.Count; $i++) {
[PSCustomObject]@{
FolderNames2 = "${i}: $($Folder2[$i].Name)"
}
}

$Folder3 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 3'
$Folders3 = for ($i=0; $i -lt $Folder3.Count; $i++) {
[PSCustomObject]@{
FolderNames3 = "${i}: $($Folder3[$i].Name)"
}
}

$Count = ($Folders1.Count,$Folders2.Count,$Folders3.Count | Measure-Object -Maximum).Maximum

& {
for ($i=0; $i -lt $Count; $i++) {
[PSCustomObject]@{
SelectionOne = ($Folders1[$i].FolderNames1 | Format-List | Out-String).Trim()
SelectionTwo = ($Folders2[$i].FolderNames2 | Format-List | Out-String).Trim()
SelectionThree = ($Folders3[$i].FolderNames3 | Format-List | Out-String).Trim()
}
}
} | Format-Table -AutoSize -Wrap

$index = Read-Host -Prompt "Enter Folder Numbers" # Selected 3, 14
$index = $i.Trim() -split ','
foreach ($i in $index) {

# here would be correct folder for the corresponding
# array number.
# example:

<#
# from the selected 3, and 14.
# the following folders are selected
'Something Here'
'Space'
#>
}

谁能指出我正确的方向?

我觉得这是以前没有做过的事情,但我觉得它对 future 的 Posh 用户会很方便。

最佳答案

非常好的脚本练习亚伯拉罕 :) 我没有做太多改变,可能有更好的方法,但我认为这应该有效。

那么,发生了什么变化?

  • 首先存储 for ($i=0; $i -lt $Count; $i++) { ... } 的结果,然后再将其显示到控制台:
$result = for ($i=0; $i -lt $Count; $i++) {...}

$result | Format-Table -Wrap
  • [int[]] 转换为 Read-Host,您需要在此处实现一个try {} catch {}:<
[int[]]$index = (Read-Host -Prompt "Enter Folder Numbers") -split ',' # Implement Try Catch here
  • 文件/文件夹的编号,如何记住最后一个索引?,可以在第一个 for 循环之后完成($rememberI = $ i) 然后在下面的循环中你只需递增 $rememberI:
[PSCustomObject]@{
FolderNames2 = "${rememberI}: $($Folder2[$i].Name)"
}
$rememberI++
  • 最后,如何找出用户选择的文件/文件夹,这有点老套,我认为这里可能有更好的方法,我不太好解释这是如何工作的,我只是知道应该工作 :P
[regex]::Match(
$result.ForEach({$_.PSObject.Properties}).Where({$_.Value -match "^${i}:"}).Value,
'(?<=: ).*'
).Value

代码

$Folder1 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 1'
$Folders1 = for ($i=0; $i -lt $Folder1.Count; $i++)
{
[PSCustomObject]@{
FolderNames1 = "${i}: $($Folder1[$i].Name)"
}
}

$rememberI = $i

$Folder2 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 2'
$Folders2 = for ($i=0; $i -lt $Folder2.Count; $i++)
{
[PSCustomObject]@{
FolderNames2 = "${rememberI}: $($Folder2[$i].Name)"
}
$rememberI++
}

$Folder3 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 3'
$Folders3 = for ($i=0; $i -lt $Folder3.Count; $i++)
{
[PSCustomObject]@{
FolderNames3 = "${rememberI}: $($Folder3[$i].Name)"
}
$rememberI++
}

$Count = ($Folders1.Count,$Folders2.Count,$Folders3.Count | Measure-Object -Maximum).Maximum

$result = for ($i=0; $i -lt $Count; $i++)
{
[PSCustomObject]@{
SelectionOne = ($Folders1[$i].FolderNames1 | Format-List | Out-String).Trim()
SelectionTwo = ($Folders2[$i].FolderNames2 | Format-List | Out-String).Trim()
SelectionThree = ($Folders3[$i].FolderNames3 | Format-List | Out-String).Trim()
}
}

$result | Format-Table -Wrap

[int[]]$index = (Read-Host -Prompt "Enter Folder Numbers") -split ',' # Implement Try Catch here

foreach ($i in $index)
{
[regex]::Match(
$result.ForEach({$_.PSObject.Properties}).Where({$_.Value -match "^${i}:"}).Value,
'(?<=: ).*'
).Value
}

编辑

显示 FullName 而不是 Name ?

$folderIndex = @{}

$Folders1 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 1' |
ForEach-Object -Begin { $i = 0 } -Process {

$newName = "${i}: $($_.Name)"

[PSCustomObject]@{
FolderNames1 = $newName
}

$folderIndex[$newName] = $_.FullName
$i++

}

$rememberI = $i

$Folders2 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 2' |
ForEach-Object {

$newName = "${rememberI}: $($_.Name)"

[PSCustomObject]@{
FolderNames2 = $newName
}

$folderIndex[$newName] = $_.FullName
$rememberI++
}

$Folders3 = Get-ChildItem -Path 'C:\users\Abraham\Desktop\Number 3' |
ForEach-Object {

$newName = "${rememberI}: $($_.Name)"

[PSCustomObject]@{
FolderNames3 = $newName
}

$folderIndex[$newName] = $_.FullName
$rememberI++
}

$Count = ($Folders1.Count,$Folders2.Count,$Folders3.Count | Measure-Object -Maximum).Maximum

$result = for ($i=0; $i -lt $Count; $i++)
{
[PSCustomObject]@{
SelectionOne = ($Folders1[$i].FolderNames1 | Format-List | Out-String).Trim()
SelectionTwo = ($Folders2[$i].FolderNames2 | Format-List | Out-String).Trim()
SelectionThree = ($Folders3[$i].FolderNames3 | Format-List | Out-String).Trim()
}
}

$result | Format-Table -Wrap

[int[]]$index = (Read-Host -Prompt "Enter Folder Numbers") -split ',' # Implement Try Catch here

foreach ($i in $index)
{
$thisVal = $result.ForEach({$_.PSObject.Properties}).Where({$_.Value -match "^${i}:"}).Value
$folderIndex[$thisVal]
}

关于powershell - 使用 PSCustomObject 在单独的列中列出多个文件夹然后选择一些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68760132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com