I was wondering how I would use a batch file or Python to open a random folder from a selection of many folders within a directory?
Asked
Active
Viewed 1,558 times
2 Answers
2
>>> import random
>>> import os
>>> files = os.listdir('/tmp')
>>> dirs = [f for f in files if os.path.isdir(f)]
>>> random.sample(dirs,1)
['tempdir']

garnertb
- 9,454
- 36
- 38
0
In cmd
you could do it like this:
@echo off
setlocal EnableDelayedExpansion
set root=C:\base\folder
for /f %%d in ('dir /b /a:d "%root%" ^| find /c /v ""') do set count=%%d
set /a num=%RANDOM% %% %count%
for /f "skip=%num% tokens=*" %%d in ('dir /b /a:d "%root%"') do (
set folder=%%~fd
goto :FIN
)
:FIN
echo %folder%
endlocal

Ansgar Wiechers
- 193,178
- 25
- 254
- 328
) 3) open directory 4) PROFIT!
– vchyzhevskyi Mar 27 '13 at 20:44