-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
  • 11,360
  • 21
  • 88
  • 132

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,250
  • 33
  • 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
  • 184,186
  • 23
  • 230
  • 299