简体   繁体   中英

Starting random batch file from a certain folder

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.bat

and so on...

I need a script that will randomly start one of those batch files when I click it. Script I need will be stored in the same folder by the name "abcRandom.bat" or something similar.

@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

Say 1000 is the number of your abcX.bat files. Then this will do the trick:

SET /a rand=%RANDOM%*1000/32768+1
CALL abc%rand%.bat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM