# Solved: batch script for randomly selecting a file in a directory.



## Yoshi8765 (Sep 1, 2010)

So I'm trying to have a script that will randomly pick a .zip file or a .rar file in a directory. I went to google and found this:

```
setlocal EnableDelayedExpansion
set count=0
for /f "delims=" %%a in ('dir /b *.mp4') do @(
	set filename[!count!]=%%a
	set /a count = count + 1
)
set /a choose = (%random% * 32768 + %random%) %% count
set chosen=!filename[%choose%]!
echo %chosen%
pause
```
Replace .mp4 with .zip and it works well!
But how would I get it to select from .zip AND .rar? Sorry, I have absolutely no coding experience.

Thanks in advance!


----------



## Squashman (Apr 4, 2003)

dir /b *.zip *.rar


----------



## Yoshi8765 (Sep 1, 2010)

Thank you! Will try test this out and report back.

Edit: Yup. Definitely works! Thanks! I have a follow-up question though-- is there a way to add folders as well? I don't know if that's possible.


----------



## Squashman (Apr 4, 2003)

Don't edit your posts, Mark it solved and then ask another question in it. It doesn't notify anyone that the thread has been updated. If you wanted to ask another question you should have posted another reply to the thread.

The current script will work with folders if it matches your search parameters. Most People don't name their folders with periods but you certainly can name a directory with your period. So with your current code, if you had a directory named MY_MUSIC.MP4 it would match the criteria.


----------



## Yoshi8765 (Sep 1, 2010)

I see, sorry. I'll do that from now on. 

And thank you for your solution. Certainly makes a lot of sense.


----------

