1

我试图用批处理更改为文件夹中的随机子目录

cd c:\*

没用,实际上每次都带你去回收站

if exist * (
cd *
)

没用

for %d in (*) do cd %d

没用

所以我很茫然,有没有办法批量做到这一点?

4

2 回答 2

2
@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%]!"
于 2013-02-24T15:19:41.567 回答
2
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"
)
于 2013-02-24T11:16:46.887 回答