问题
After having ripped all my mother's DVD library to a Drobo-FS I setup for her, she was faced with the problem that having so many choices just a mouse-click away from made it too hard to choose. My solution was a Powershell script that starts playing a random movie:
$files = Get-Childitem -Recurse -Path "\\DROBO-FS\Videos\Movies" -include *.mkv,*.avi,*.mp4,*.m4v
$ran = Get-Random -minimum 0 -maximum ($files.length - 1)
$movie = $files[$ran]
& "C:\Program Files (x86)\MPC-HC\mpc-hc.exe" $movie
The problem now is that there are a few movies in there she doesn't want to show up randomly, things she only watches on certain occasions or simply doesn't like.
My solution to this would be a text file into which she could write the names of the movies she doesn't want as part of the random choices. My problem is that I am not quite sure how to read in the names in the text file and then filter them out of the results gotten from Get-Childitem.
回答1:
Put the file names of the movies you want to exclude in a file (one name.ext per line) and pass the content of the file to the Exclude parameter:
$exclude = Get-Content .\exclude.txt
$files = Get-Childitem -Recurse -Path "\\DROBO-FS\Videos\Movies" -Include *.mkv,*.avi,*.mp4,*.m4v -Exclude $exclude
$movie = $files | Get-Random
& "C:\Program Files (x86)\MPC-HC\mpc-hc.exe" $movie.FullName
来源:https://stackoverflow.com/questions/9044514/fliter-results-of-gci-with-strings-from-a-textfile-in-powershell