Fliter results of gci with strings from a textfile in Powershell

那年仲夏 提交于 2021-01-27 17:42:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!