Reputation: 11
Im trying to change to a random subdirectory in a folder with batch
cd c:\*
doesnt work, in fact takes you to the recycle bin each time
if exist * (
cd *
)
didnt work
for %d in (*) do cd %d
didnt work
so im at a loss, is there any way to do this in batch?
Upvotes: 1
Views: 240
Reputation: 67216
@echo off
setlocal EnableDelayedExpansion
rem Create an array of dir. names
set n=0
for /D %%a in (c:\*) do (
set /A n+=1
set dir[!n!]=%%a
)
rem Select a random element from the array
set /A d=%random%*n/32768+1
rem And CD to it
cd "!dir[%d%]!"
Upvotes: 2
Reputation: 24466
setlocal enabledelayedexpansion
set c=0
rem count dirs in c:\
for /d %%I in (c:\*) do set /a c+=1 >NUL
set /a c=%RANDOM% * %c% / 32768 + 1 >NUL
set loop=0
for /d %%I in (c:\*) do (
set /a loop+=1
if !loop!==%c% cd /d "%%I"
)
Upvotes: 2