-2

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
asked Sep 25, 2015 at 8:43
2
  • what is the last number ? are all the scripts really named abcX.bat? Commented Sep 25, 2015 at 8:44
  • Sorry if i didnt made it clear...i need to create a batch script named abcRandom.bat,because that is the file i need you guys to do for me.Now,i have my own scripts named abc1.bat abc2.bat abc3.bat and so on,and that number is growwing from time to time.so far i have 112 of those files stored in the same folder.I need a script that will randomly select and call just one one of the 112 files.I hope that comes more clear.I used abc letters so it would be easyer for me to explain :D Commented Sep 25, 2015 at 9:23

2 Answers 2

4
@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
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
for some reason it didnt do the trick :( ,i marked the one that helped
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 :)
1

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

5 Comments

That's it,thanx mate:D
now,one more thing... what woould that code look like if it would have more characters,something like "word_abc_OtherWord.bat?
Just put %rand% whereever the numer should be, eg. word_abc123_OtherWord.bat will become word_abc%rand%_OtherWord.bat.
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 :-)
@MichaelS - I think it's your turn now (stackoverflow.com/questions/32780441) :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.