# Solved: Get random folder from directory using batch?



## chinagreenelvis (Mar 31, 2013)

Currently using this script to get random files in a directory:


```
@Echo Off

:Start
set count=0
for /f %%f in ('dir "C:\Folder" /b') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0

for /f "tokens=1* delims=:" %%I in ('dir /a-d /b ^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J

:Found
echo %filename%
```
I need to modify it to select a random subfolder before selecting a file in that folder. If anyone can help, I'd be grateful.


----------



## chinagreenelvis (Mar 31, 2013)

Nevermind, figured it out. Just needed to include subdirectories and tweaked the way the directory is defined so the batch can be run from anywhere.


```
@Echo Off

:Start
set directory="C:\Folder"
set count=0
for /f %%f in ('dir "%directory%" /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0

for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /a-d /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J

:Found
echo %filename%
pause
goto Start
```


----------

