随机执行 35 个批处理文件

随机执行 35 个批处理文件

我在同一个文件夹中有 35 个批处理文件。所有批处理文件都有不同的名称和代码,我想创建批处理文件,从中可以随机运行 35 个批处理文件,而无需知道它们的名称或任何特征。我创建了这个批处理文件

@echo off
    :loop
    SETLOCAL ENABLEDELAYEDEXPANSION
    if exist "C:\Users\Administrator\Desktop\converted\*.bat" (
    for %%b in ("C:\Users\Administrator\Desktop\converted\*.bat") do (
    set /a n=!random! %%35+1 
    start "" /w "C:\Users\Administrator\Desktop\!n!.bat" "%%b"
    ping -n 20 localhost >nul
    del "%%b"
    )
    )
    ping -n 60 localhost >nul
    goto :loop 

但在这种情况下,我需要更改所有批处理文件的名称,这是不推荐的。寻找解决方案。

答案1

您不想随机运行随机选择批处理文件的主脚本。最简单的解决方案是将“脚本”文件夹挂在主脚本所在的文件夹中,并将 35 个 bat 脚本放在那里。

以下解决方案适用于任意数量的脚本 - 不限于 35 个。将随机运行的次数作为第一个也是唯一的参数传递。如果未提供,则默认为 100。

@echo off
setlocal disableDelayedExpansion

:: Establish the number of runs from the first parameter. Default to 100 if not provided
set "runCount=%~1"
if not defined runCount set runCount=100

:: Set the current directory to the "scripts" folder at the location of this running script
pushd "%~dp0scripts"

:: Define numbered variables to hold all file names, and establish the total number of files
for /f "delims=: tokens=1,2" %%A in ('dir /b *.bat^|findstr /n "^"') do set "file_%%A=%%B" & set /a cnt=%%A

:: Randomly run one of the files %runCount% times
setlocal enableDelayedExpansion
for /l %%N in (1 1 %runCount%) do (
  set /a "n = !random!%%cnt + 1"
  for %%N in (!n!) do call !file_%%N!
)

popd

答案2

你可以这样做:

@echo off
pushd "Directory"
setlocal enabledelayedexpansion
set /a r=%random% %% 35 + 1
set n=0
:loop
set /a n=n+1
if !n!==35 goto :eof
for /f "tokens=* delims= " %%a in ('dir /b *.bat') >t.txt
for /f "tokens=1* delims=[]" %%a in ('find /n /v "" <t.txt ^| find '!r!'") Do "%%~a"
goto loop

答案3

@ECHO OFF
Set Prog_Dir=%~dp0

CD %Prog_Dir%\Subfolder

SETLOCAL ENABLEDELAYEDEXPANSION
set count=0

:reset

REM - Dual random comparison IF Check used to thoroughly randomise the launch of .Bats
REM - Check Call and reset loop used to ensure exactly 35 .Bats launched.

For %%c IN (*.bat) DO (
                Set /a rand1=!random! * 35 / 32768 +1
                Set /a rand2=!random! * 114 / 32768 +1
                IF "!rand1!"=="!rand2!" (
            Call :CHECK
            START "" %%c
    )

)

GOTO reset

:CHECK
Set /a count+=1
IF !count!==36 GOTO end
GOTO :EOF

:end
ECHO 35 batches randomly Launched.
pause
exit

答案4

我得到了这个代码,但它会随机选择并运行文件夹和子文件夹中的所有文件,它对我有用,只需将您的 35 个 .bat 文件放在一个文件夹中,您只需要用另一个批处理循环该程序,它的无限 rnd 打开

这是一个文件.bat

@echo off & setlocal

 set "workDir=C:\DVDCOVERS"

 @set /a "rdm=%random%"
 set /a "rdm=%random%"

 pushd "%workDir%"

 set /a "counter=0"
 for /f "delims=" %%i in ('dir /b ^|find "."') do call :sub1

 set /a "rdNum=(%rdm%*%counter%/32767)+1"

 set /a "counter=0"
 for /f "delims=" %%i in ('dir /b ^|find "."') do set "fileName=%%i" &call :sub2
h.
 popd "%workDir%"

 goto :eof

 :sub1
 set /a "counter+=1"
 goto :eof

 :sub2
 set /a "counter+=1"
 if %counter%==%rdNum% (start "" "%fileName%")
 goto :eof

然后创建另一个 .bat 文件

@echo off

:start_rnd
start file.bat
goto :start_rnd

不过它不是那么干净,如果你想让它们一直在一起,你可能需要将它们绑定在一起

相关内容