1

バッチのあるフォルダ内のランダムなサブディレクトリに変更しようとしています

cd c:\*

動作しません、実際には毎回ごみ箱に移動します

if exist * (
cd *
)

動作しませんでした

for %d in (*) do cd %d

動作しませんでした

途方に暮れているので、これをバッチで行う方法はありますか?

4

2 に答える 2

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 に答える
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 に答える