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.
vhu
12.9k11 gold badges42 silver badges50 bronze badges
2 Answers 2
@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
answered Sep 25, 2015 at 8:56
Paul
2,6982 gold badges20 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Slobodan Kovačević
Thanx bro,but this didnt work for me because im missing abcRandom.bat file...That is the one i need you guys for to do :) i doesnt have to be called like that,name isnt important,i can name it anyhow,right?
Slobodan Kovačević
I am aware of that,but in youre script you used that abcRandom.bat file that i actualy need from you guys... i dont have it,i need it...NVM,point is to call randomly one abc.x file,x stands for number.
Slobodan Kovačević
for some reason it didnt do the trick :( ,i marked the one that helped
Slobodan Kovačević
Bro,youre a genious,but i found what was blocking me in youre script... The last line shouldnt be "echo start !f[%c%]!" but it should be "start !f[%c%]!" :D Please,edit youre post and i will gladly mark it,just remove "echo" from the last line :)
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
answered Sep 25, 2015 at 9:04
MichaelS
6,0807 gold badges33 silver badges47 bronze badges
5 Comments
Slobodan Kovačević
That's it,thanx mate:D
Slobodan Kovačević
now,one more thing... what woould that code look like if it would have more characters,something like "word_abc_OtherWord.bat?
MichaelS
Just put
%rand% whereever the numer should be, eg. word_abc123_OtherWord.bat will become word_abc%rand%_OtherWord.bat.MichaelS
This is a whole new problem which requires a different solution. Please post it as a new question and I'll give you the code :-)
Bö macht Blau
@MichaelS - I think it's your turn now (stackoverflow.com/questions/32780441) :)
abcX.bat?