Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Astuce/Batch] Choix random d'une playlist

Sujet résolu
    15 décembre 2019 à 15:34:33

    Bonjour.

    (Nota: Je ne sais pas si c'est le bon endroit pour ce post, je laisse soin à un modérateur d'en décider...

    Si comme moi vous disposez d'un certain nombre de musiques sur votre système, et que vous ne savez parfois pas trop quelle playlist écouter, le script batch (.bat) suivant choisi aléatoirement l'une de vos playlists.

    Pour utiliser ce script à la racine d'une clé USB, enlevez le "\" de "%CD%\Playlists", à la ligne 7 (anglais) ou 5 (français).

    Playlist random choice.bat (en anglais et avec commentaires) :

    @echo off
    setlocal EnableDelayedExpansion
    :: Delete a possible "m3u_list.txt" file.
    set list=%TEMP%\m3u_list.txt
    if exist %list% del /f /q %list%
    :: If there's a "Playlists" folder in the current folder, this script will use it; if there's no "Playlists" folder in the current folder, this script will use the "%USERPROFILE%\Music" folder (example: "C:\Users\User Name\Music").
    if exist %CD%\Playlists (set musicFolder=%CD%) else (set musicFolder=%USERPROFILE%\Music)
    :: Create the M3U list.
    set playlists=%musicFolder%\Playlists
    for /r %playlists% %%X in (*.m3u) do (echo %%X >> %list%)
    
    :random
    cls
    :: Count the number of M3U.
    set /a counter=0
    for /f "delims=" %%i in ('type %list%') do set /a counter+=1
    :: Generate a random number between 0 and the "number of M3U -1" (number 0 = line 1 of the M3U list).
    set /a randomNumber=%random% %%%counter%
    :: Get the playlist URL corresponding to the random number.
    set playlist=
    for /f "delims=" %%a in ('more /e +%randomNumber% ^< %list%') do (if not defined playlist set "playlist=%%a")
    :: Count the characters in the URL of the "Playlists" folder (with the final "\").
    for /f "tokens=1,* delims=[,]" %%A in ('"%comspec% /u /c echo:%playlists%|more|find /n /v """') do set /a playlistsURLcharNumber=%%A-3
    :: Get the playlist name in the M3U list, by removing the URL of the "Playlists" folder (with the final "\") at the line's begining, and removing the ".m3u" extention + white space at the line's end.
    set "playlist=!playlist:~playlistsURLcharNumber,-5!"
    :: Escape of possible "&" and "," characters in the playlist name.
    set playlist=%playlist:&=^^^&%
    set playlist=%playlist:^,=^^^,%
    :: Get the playlist number (line 1 of the M3U list = number 1).
    set /a listLine=%randomNumber%+1
    
    :: On screen: Playlist number / number of remaining playlists in the M3U list (see line #76 of this script) : playlist name
    echo Playlist %listLine%/%counter% : !playlist!
    :: What to do?
    echo.
    echo Possible choices:
    echo.
    set quit=[0] Quit.
    echo %quit%
    set open=[1] Open the music folder.
    echo %open%
    set other=[2] Select another Playlist.
    echo %other%
    echo [3] Start this Playlist.
    echo.
    choice /c 1230 /n /m "What's your choice?"
    set choice=%errorlevel%
    :: [1] Open the music folder, delete the "m3u_list.txt" file and close this window.
    if %choice%==1 (start "%musicFolder%" "%musicFolder%" & del /f /q %list% & exit)
    :: [2] Select another Playlist.
    if %choice%==2 goto :anotherPlaylist
    :: [3] Start the Playlist.
    if %choice%==3 (start "!playlist!" "%playlists%\!playlist!.m3u" & goto :playing)
    :: [0] Quit.
    if %choice%==4 exit
    
    :: On screen - choice [3]
    :playing
    cls
    echo Playing "!playlist!"
    echo (you can minimize this window)...
    echo.
    echo At anytime you can:
    echo.
    :: [0] Quit; [1] Open the music folder; [2] Select another Playlist.
    echo %quit%
    echo %open%
    echo %other%
    echo.
    choice /c 120 /n /m "What's your choice?"
    set choice=%errorlevel%
    if %choice%==1 (start "%musicFolder%" "%musicFolder%" & del /f /q %list% & exit)
    if %choice%==2 goto :anotherPlaylist
    if %choice%==3 exit
    
    :: [2] Select another Playlist.
    :anotherPlaylist
    cls
    :: Remove the previous playlist in the M3U list.
    findstr /v /c:"%playlists%\%playlist%.m3u " < %list% > %tempList%
    :: Write the remaining playlists in a new M3U list, delete the old M3U list, and return to the line #11 of this script to select another playlist.
    copy %tempList% %list%
    del /f /q %tempList%
    goto :random

    Choix aléatoire d'une playlist.bat (en français, encodage "OEM 863", et sans commentaires) :

    @echo off
    setlocal EnableDelayedExpansion
    set list=%TEMP%\liste_m3u.txt
    if exist %list% del /f /q %list%
    if exist %CD%\Playlists (set musicFolder=%CD%) else (set musicFolder=%USERPROFILE%\Music)
    set playlists=%musicFolder%\Playlists
    for /r %playlists% %%X in (*.m3u) do (echo %%X >> %list%)
    
    :random
    cls
    set /a counter=0
    for /f "delims=" %%i in ('type %list%') do set /a counter+=1
    set /a randomNumber=%random% %%%counter%
    set playlist=
    for /f "delims=" %%a in ('more /e +%randomNumber% ^< %list%') do (if not defined playlist set "playlist=%%a")
    for /f "tokens=1,* delims=[,]" %%A in ('"%comspec% /u /c echo:%playlists%|more|find /n /v """') do set /a playlistsURLcharNumber=%%A-3
    set "playlist=!playlist:~%playlistsURLcharNumber%,-5!"
    set playlist=%playlist:&=^^^&%
    set playlist=%playlist:^,=^^^,%
    set /a listLine=%randomNumber%+1
    echo Playlist %listLine%/%counter% : !playlist!
    echo.
    echo Choix possibles :
    echo.
    set quit=[0] Quitter.
    echo %quit%
    set open=[1] Ouvrir le dossier de musique.
    echo %open%
    set other=[2] Choisir une autre Playlist.
    echo %other%
    echo [3] Lire cette Playlist.
    echo.
    choice /c 1230 /n /m "Quel est votre choix ?"
    set choice=%errorlevel%
    if %choice%==1 (start "%musicFolder%" "%musicFolder%" & del /f /q %list% & exit)
    if %choice%==2 goto :anotherPlaylist
    if %choice%==3 (start "!playlist!" "%playlists%\!playlist!.m3u" & goto :playing)
    if %choice%==4 exit
    
    :playing
    cls
    echo Lecture de "!playlist!" en cours
    echo (vous pouvez réduire cette fenêtre)...
    echo.
    echo A tout moment vous pouvez :
    echo.
    echo %quit%
    echo %open%
    echo %other%
    echo.
    choice /c 120 /n /m "Quel est votre choix ?"
    set choice=%errorlevel%
    if %choice%==1 (start "%musicFolder%" "%musicFolder%" & del /f /q %list% & exit)
    if %choice%==2 goto :anotherPlaylist
    if %choice%==3 exit
    
    :anotherPlaylist
    cls
    findstr /v /c:"%playlists%\%playlist%.m3u " < %list% > %tempList%
    copy %tempList% %list%
    del /f /q %tempList%
    goto :random

    Enjoy! :)

    -
    Edité par TheScar.fr 15 juin 2021 à 14:52:44

    • Partager sur Facebook
    • Partager sur Twitter

    Windows 7 Professionnel SP1 x64

    .

      20 décembre 2019 à 13:08:45

      J'ai partagé ce script sur DeviantArt, et en ferait peut-être de même pour d'autres scripts.

      -
      Edité par TheScar.fr 20 décembre 2019 à 13:10:00

      • Partager sur Facebook
      • Partager sur Twitter

      Windows 7 Professionnel SP1 x64

      .

        23 décembre 2019 à 18:47:34

        DevianArt ne me semble pas très adapté au code. Une forge logiciel comme github ou gitlab ferrait mieux l'affaire.

        • Partager sur Facebook
        • Partager sur Twitter
          24 décembre 2019 à 12:13:40

          Merci mais non merci, ces sites sont trop compliqués, trop confus, trop peu ergonomiques et en Anglais...

          J'utilise déjà DeviantArt, qui même s'il est en Anglais reste accessible ; du coup mon choix est fait.

          • Partager sur Facebook
          • Partager sur Twitter

          Windows 7 Professionnel SP1 x64

          .

            14 août 2020 à 15:24:28

            Script édité pour ajouter l’échappement de virgules dans le nom de la playlist.
            • Partager sur Facebook
            • Partager sur Twitter

            Windows 7 Professionnel SP1 x64

            .

              30 avril 2021 à 20:44:23

              Script amélioré.

              De plus, tant qu'ils sont tous les deux dans le même dossier, il n'est plus nécessaire que le fichier .bat et le sous-dossier "Playlists" soient dans le dossier "%USERPROFILE%\Music".

              -
              Edité par TheScar.fr 2 mai 2021 à 19:31:08

              • Partager sur Facebook
              • Partager sur Twitter

              Windows 7 Professionnel SP1 x64

              .

                15 juin 2021 à 14:53:49

                Ajout de la mention suivante :

                Pour utiliser ce script à la racine d'une clé USB, enlevez le "\" de "%CD%\Playlists", à la ligne 7 (anglais) ou 5 (français).

                • Partager sur Facebook
                • Partager sur Twitter

                Windows 7 Professionnel SP1 x64

                .

                [Astuce/Batch] Choix random d'une playlist

                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                • Editeur
                • Markdown