如何让 Paul Black 的随机文件选择器批处理脚本不选择自身

如何让 Paul Black 的随机文件选择器批处理脚本不选择自身

我一直在使用 Paul Black 的随机文件选择器批处理脚本来处理我们广播电台播放的一个方面 - 而且大多数时候它运行良好。

除非偶尔它会自行添加,因为当然,它必须位于同一文件夹中。此时播放会停止,因为 .bat 不是音频文件。

这是我修改后的原始脚本,用于通过 CMD 将文件推送到我们的播放设备:

@echo off
:: ##############################################################################
:: # Title:      Select a random filename in the current directory [ folder ].  #
:: # Created By: Paul Black at TenForums.com/members/paul-black.html            #
:: # Created:    October 2020.                                                  #
:: ##############################################################################
title Select A Random Filename.
mode con: cols=80 lines=5 & color 17
setlocal EnableDelayedExpansion
set n=0
for %%f in (*.*) do (
  set /A n+=1
  set "file[!n!]=%%f"
)
set /A "RAND=(n*%Random%)/32768+1" & set /A "rand=(n*%Random%)/32768+1"
echo. & echo "!file[%RAND%]!"
echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & exit

那么我怎样才能让它忽略自身,而不是选择呢random.bat

答案1

@Giacomo1968 回答有正确的想法;(*.*)在您的FOR声明中更改以排除非音频文件(或者更确切地说,仅包含音频文件)。

FOR 上的 SS64请注意,您可以有多个模式,包括由标准分隔符分隔的通配符。

因此,如果您希望将选择范围限制为(例如).WAV仅限.MP3文件,则可以这样写:

FOR %%f IN (*.MP3,*.WAV) ...

我建议将 SS64 加入书签;它是 Linux 和 macOS 上 Winbatch、PowerShell 和 bash 的简洁而详细的参考。

答案2

这里已经有一个很好的答案,但是假设您希望您的脚本包含每个文件但不包含您的脚本,那么以下更改将明确跳过您的脚本:

您提到的脚本名称是random.bat

@echo off
:: ##############################################################################
:: # Title:      Select a random filename in the current directory [ folder ].  #
:: # Created By: Paul Black at TenForums.com/members/paul-black.html            #
:: # Created:    October 2020.                                                  #
:: ##############################################################################
title Select A Random Filename.
mode con: cols=80 lines=5 & color 17
setlocal EnableDelayedExpansion
set n=0
for %%f in (*.*) do (
  if not "%%f"=="random.bat" set /A n+=1
  if not "%%f"=="random.bat" set "file[!n!]=%%f"
)
set /A "RAND=(n*%Random%)/32768+1" & set /A "rand=(n*%Random%)/32768+1"
echo. & echo "!file[%RAND%]!"
echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & exit

在遍历所有文件的 for 循环中,我们检查当前迭代是否为 random.bat。如果不是,我们将执行您的代码。

答案3

:: ###############################################################################
:: #      Title:  Select a random filename in the current directory [ folder ].  #
:: # Created By:  Paul Black at TenForums.com/members/paul-black.html            #
:: #    Created:  October  2020.                                                 #
:: #     Edited:  December 2023.        https://superuser.com/a/1823108/969781   #
:: ###############################################################################

@echo off && setlocal EnableDelayedExpansion

set /a "_rand=0, _n=1" & mode.com con: cols=80 lines=05
title <nul & title Select A Random Filename. & color 17

for /f usebackq^delims^= %%G in =;(`
    where "%~dp0:*.*" ^| findstr /vei "\.cmd \.bat"
   `);= do set "_file_[!_n!]=%%~nxG" & set /a "_n+=1"
   
call set /a "_rand=(%%random:-=%% %%%% (!_n! - 1) + 1)"
>con: echo; & echo;"!_file_[%_rand%]!" & echo; & endlocal
timeout.exe /t -1 | set /p "`=>Press ANY key to EXIT . . ." 

或者...

:: ###############################################################################
:: #      Title:  Select a random filename in the current directory [ folder ].  #
:: # Created By:  Paul Black at TenForums.com/members/paul-black.html            #
:: #    Created:  October  2020.                                                 #
:: #     Edited:  December 2023.        https://superuser.com/a/1823108/969781   #
:: ###############################################################################

@echo off && setlocal EnableDelayedExpansion

set "_eXt=*.opus *.m4a *.mp3 *.flac *.aiff *.aac *.ogg *.wma"
set /a "_rand=0, _n=1" & mode con:cols=80lines=5 & cd /d "%~dp0"
title <nul & title Select A Random Audio Filename... && color 17

for /f usebackq^delims^= %%G in =;(` where "!cd!":!_eXt! 2^>nul
    `);= do set "_file_[!_n!]=%%~nxG" & set /a "_n+=1"

call set /a "_rand=(%%random:-=%% %%%% (!_n! - 1) + 1)"
>con: echo; & echo;"!_file_[%_rand%]!" & echo; & endlocal
timeout.exe /t -1 | set /p "`=>Press ANY key to EXIT..." & exit

如何让 Paul Black 的随机文件选择器批处理脚本不选择自身?

将 simple 替换for为,for /f以便其输出通过: 或者明确指定音频扩展名来过滤文件,而无需 选择您的
list your files | findstr /aVoid /Insensitive /End ".cmd .bat"

for /f循环


@echo off && setlocal EnableDelayedExpansion

cd /d "%~dp0" & set /a "_skip=0, _nX=0"

title <nul & title Select A Random Filename.
mode.com con: cols=00080 lines=05 & color 17

for /f %%X in =;(' dir /a /b ^| find /v "%~x0" ^| find /c /v ""
    ');= do set /a "_nX=%%X-1, _skip=%random% %% %%X - 1"

if !_skip! equ 0 =;( 
        for /f delims^= %%f in =;(' dir /a:-d /b ^| find /v "%~x0"');= do set "_fn=%%~f" & echo. & goto %:^) 
    );= else =;( 
        for /f "skip=%_skip:-=% delims=" %%f in =;(' dir /a:-d /b ^| find /v "%~x0" ');= do set "_fn=%%~f" & echo. & goto %:^)
    );=
    
%:^)
echo/"!_fn!" & echo/ & timeout -1 | =;(
     set /p "`=> Press ANY key to EXIT . . ." & endlocal & exit
    );=


  1. 通过跳过 bat 来计算文件夹中有多少个文件
  2. 获取范围内的随机数0 - [ total files - 1 ]
  3. For在范围内随机选择一个,然后使用循环直接跳转到文件skip={random range -1}
  4. 无需为每个文件名称仅选择一个全部定义之后

建议进行 PowerShell 调用以在单个循环中选择文件:

@echo off & cd /d "%~dp0"
 
title <nul & title Select A Random Filename.
mode.com con: cols=00080 lines=05 & color 17

for /f usebackq^delims^= %%F in =;(`
    "PowerShell.exe Get-Random (gci ".\*" -ex *.cmd,*.bat).Name"
    `);= do echo/ & echo/"%%~nxF" & echo/ & timeout -1 |;= =;(
         set /p "`=> Press ANY key to EXIT . . ." );= & exit

答案4

尝试改变这个:

for %%f in (*.*) do (

要将其限制为.mp3这样的文件:

for %%f in (*.mp3) do (

或者只是更改该.mp3扩展名以更好地匹配您的文件命名约定。

相关内容