-4

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136

2 Answers2

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