Starting random batch file from a certain folder

后端 未结 2 1440
南旧
南旧 2021-01-29 11:41

Problem is this, I have one folder named \"abc\" with several batch files and they are named like this:

abc1.bat
abc2.bat
abc3.bat
abc4

2条回答
  •  不要未来只要你来
    2021-01-29 11:54

    @echo off
    setlocal EnableDelayedExpansion & set n=0
    for /f "delims=" %%a in ('dir /b /A-D "*.bat"') do (
       (echo "%%a" | FIND /I "%~nx0" 1>nul) || (
           set "f=%%a" & set "f[!n!]=!f!" & set /a "n+=1"
       )
    )
    set /a c=%random% %% n
    echo start !f[%c%]!
    

    You can get an explanation how it works from https://stackoverflow.com/a/32700063/4070433

提交回复
热议问题