0

For learnig Powershell I've wrote a script that choose a film randomly from a list, until the list runs out, but, at the end of list I want that the shell keeps open... I've tried many options Break/Return/exit... nothing to do... tried to add the last if condition in to the while condition but, not works, at the end of list an error message says that the array is empty/null, or shell quits after last film...

Yes, I can put a pause instead of return, for that script would be ok, but it doesn't seems an "elegant" solution...

The icing on the cake would be a better option to trim the extension from film name..! Because in my mode, a film name that finish with a "m", "k", or "v", these chars are trimmed out toghether with the ".mkv" expression! :)

Can someone drive me please?

This is the code:

$LastFile = (Get-Item "F:\DATA\Films list\*" | Where-Object {$_.Name -match "Title"} | sort LastWriteTime | select -last 1).FullName
$TitleList = Get-Content $LastFile | where { $_ -notmatch "4K" }
$TitleList.Count

$Listed = New-Object System.Collections.ArrayList
Foreach ( $film in $TitleList ) {
    $Listed.Add("$film") | out-null
    }
$RandomFilm = Get-Random -InputObject $Listed
Write-Host $RandomFilm.Trim('.mkv') -ForegroundColor Green
$Listed.Remove("$RandomFilm") 
$TitleCount = 1

echo "_________________________________________________________________________________________"
echo "`n`n" 
$Answer = Read-Host "RETURN for new film, q for quitting..." 

while ( "q" -notcontains $Answer) {
    echo "`n`n"
    $RandomFilm = Get-Random -InputObject $Listed 
    if ( $RandomFilm -ne $null ) {
        Write-Host $RandomFilm.Trim('.mkv') -ForegroundColor Green
        $Listed.Remove("$RandomFilm")
        $TitleCount++
        $TitleCount
        if ( $TitleCount -gt $TitleList.Count ) {
            Write-Host "END OF LIST..." -ForegroundColor red
            Return
            }
        else {
            echo "_________________________________________________________________________________________"
            echo "`n`n"
            $Answer = Read-Host "RETURN for new film, q for quitting..."
            }
    }
}   
ilRobby
  • 69
  • 2
  • 10

1 Answers1

0

Several ways of doing this. If you are running your script with powershell then $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")at the end of the script is a good option (this will not work with powershell_ise tho). In ISE you can add a read-host at the end. If you are running your script from a shortcut you can add -NoExit: powershell.exe -NoExit -file .\script.ps1

The icing on the cake would be a better option to trim the extension from film name..! Because in my mode, a film name that finish with a "m", "k", or "v", these chars are trimmed out toghether with the ".mkv" expression! :)

Im not quite sure I understood this part, could you please elaborate.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 1
    `if ( $RandomFilm.EndWith(".mkv") { $RandomFile = $RandomFile.Remove($RandomFile.Length-4) }` – Tim Roberts Mar 08 '21 at 23:41
  • @santisq, Hi santis, tried `$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")`, seems not works, shell still quitting... About the "icing..", I mean that "The Mask.mkv" is trimmed like this "The Mas" ! – ilRobby Mar 08 '21 at 23:41
  • https://stackoverflow.com/questions/20886243/press-any-key-to-continue check this link. Regarding the trimming question, if all files are ".mkv" you can use `'The Mask.mkv' -replace '.mkv'` or `'The Mask.mkv'.replace('.mkv','')` or `'The Mask.mkv'.split('.')[0]` – Santiago Squarzon Mar 08 '21 at 23:44
  • @Tim Roberts Thanks Tim, perfect! Just for who "copy and paste" will be: `$Random**Film**.Remove($Random**Film**.Length-4)` :) – ilRobby Mar 08 '21 at 23:48
  • @santisq, Yes santis, as I sayd `pause` is a solution, but I prefer to learn another/better/correct techique! – ilRobby Mar 08 '21 at 23:50
  • There are plenty of references to your question on the link I pasted as well as on this https://stackoverflow.com/questions/3963100/how-do-you-do-a-pause-with-powershell-2-0 please read. – Santiago Squarzon Mar 08 '21 at 23:55
  • @santisq, I've read it, I swear!! But all that methods inserts a "pause" or an answer... I think there must be a way to exit from a while loop and keep shell active... but, of course, I can be wrong! – ilRobby Mar 09 '21 at 00:06
  • if you want the shell still active after the loop finishes then you can either create a shortcut to your script adding the `-NoExit` flag or run the script with powershell_ise. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1 – Santiago Squarzon Mar 09 '21 at 00:13