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..."
}
}
}