1

I have this script right here:

$directory = "C:\New Folder"
$counter = 1

Set-Location $directory

#Menu
Write-Host "==========" -BackgroundColor DarkGreen
Write-Host "===MENU===" -BackgroundColor DarkGreen
Write-Host "==========" -BackgroundColor DarkGreen
#Shows only .wav files and it order them
Get-ChildItem -Name -Include *.wav | ForEach-Object {Write-Host "[$counter] " -ForegroundColor Green -NoNewLine; Write-Host "$_"; $counter++}
Write-Host ""

$input = Read-Host -Prompt "Select a file from the menu"
Clear-Host
Write-Host "You have just selected: $input"

At this point i would like to display the name of the selected file. How could i do that?

Dstr0
  • 95
  • 2
  • 13
  • 3
    instead of using a counter, just use the array index. then you can call up any item by `$FileList[$Index]`. for an example, lookee ... function_Get-MenuChoice [simplified] - Pastebin.com — https://pastebin.com/KMu2G9Dq – Lee_Dailey Apr 27 '21 at 18:50
  • @Lee_Dailey It's a little too tricky for me, i don't understand that much, but thank you anyway. I appreciate it – Dstr0 Apr 27 '21 at 20:40
  • you are most welcome! [*grin*] i think my solution is simpler, but it works on the same basic idea. in any case, i am glad to know that you got things working as needed. – Lee_Dailey Apr 27 '21 at 21:22

2 Answers2

4

Although an answer has been accepted, I will demonstrate how you can make a selection out of files using the for loop.

We first have to start by gathering the files using Get-ChildItem like so:

$WavFiles = Get-ChildItem -Filter "*.wav"

Notice how I didn't just narrow it down to JUST names? This gives us more options to work with such as, adding the last write time to our selection menu so we can see when they were last worked on.

Next, we will create a PSCustomObject to store our results and make a listing out of them using a for loop to assigning a number to each file name based off our gathered items in $WavFiles.

$(for($i=0; $i -lt $WavFiles.Count; $i++){
    [PSCustomObject]@{
        'File Name'       = "${i}: $($WavFiles.BaseName[$i])"
        'Last Write Time' = $WavFiles.LastWriteTime[$i]
            }
        }) | Out-Host

If you're familiar on how to make a selection out of an array ( $array[3] ), you should be able to peace together whats happening when we iterate though our collection of $WavFiles simply by assigning it, its corresponding array number. The $() | Out-Host is added due to Read-Host taking precedence and displaying before the output of our object; this is a work around for that which you can read more about it here.

Lastly, we ask for user input with our Read-Host assigning the input value to $input which we will use to once again, reference the corresponding file using the number its assigned in our collection of $WavFiles

$input = Read-Host -Prompt "Select a file from the menu" Clear-Host Write-Host "You have just selected: $($WavFiles[$input])"

The $input variable becomes the number selected which is why we can use it to reference the corresponding value in the array.

It all comes together like so:

#$directory = "C:\New Folder"
#Set-Location $directory

$WavFiles = Get-ChildItem -Filter "*.wav"
    
    $(for($i=0; $i -lt $WavFiles.Count; $i++){
        [PSCustomObject]@{
            'File Name'       = "${i}: $($WavFiles.BaseName[$i])"
            'Last Write Time' = $WavFiles.LastWriteTime[$i]
                }
            }) | Out-Host

$input = Read-Host -Prompt "Select a file from the menu"
Clear-Host
Write-Host "You have just selected: $($WavFiles[$input])"

Output:


File Name                 Last Write Time      
---------                 ---------------      
0: Eula                   3/2/2011 9:48:57 AM  
1: psversion              3/2/2011 9:48:56 AM  
2: readme                 11/6/2020 12:57:53 PM
3: ThirdPartyNoticesBySHS 2/27/2021 10:31:01 PM
4: WindowsCodecsRaw       3/18/2019 10:46:13 PM
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • 1
    Your version also works well - thanks for sharing. The `for` loop will be familiar to someone who has worked in Javascript or C#. You can also use the `%` (alias for `foreach`) syntax with an initializer `{$i=0}` to get slightly more terse Powershell code: `$WavFileList = get-childitem *.wav|%{$i=0}{[PSCustomObject]@{Index = $i++;Name=$_.Name;Modified=$_.LastWriteTime;}}` – Rich Moss Apr 27 '21 at 23:34
3

I created function for this purpose that I described here.

It may be overkill for your needs but you can pass the set of *.wav files with this code:

Show-SimpleMenu (get-childitem *.wav).Name

Please feel free to report any issues on GitHub, or here.

Rich Moss
  • 2,195
  • 1
  • 13
  • 18