2017-03-13 10 views
1

おはよう!だから私は、重いシステムの制限のためにバッチで達成しなければならないこの仕事を持っています。バッチ内のランダムファイル選択「検証」

これは、フォルダからファイルをランダムに選択し、別のフォルダにコピーすることです。 問題は、「ランダム性」が同じファイルを選択することがあるため、必要な10個のファイルのうち8-9個になります。 私は "検証"システムを作ろうとしましたが、ループで動作させるように見えないので、ちょうどそれは一度だけであり、同じファイルを選択する可能性はまだあります。コードがある場合

申し訳ありませんが、完全なスパゲッティ、イムないプログラマ

私は解決策が本当に簡単であること、そして私はちょうど それ助け任意の種類を参照してくださいいけない、またはアドバイスは大歓迎されていることを感じています は、事前にありがとう

"検証" は、異なるロジック使って "SUB3"

@echo off & setlocal enableextensions disabledelayedexpansion 
set "workDir=C:\Generator\Tickets" 
FOR /L %%n in (1,1,10) DO call :main %%n 
goto :sub3 

:main 
@set /a "rdm=%random%" 
set /a "rdm=%random%" 
pushd "%workDir%" 
set /a "counter=0" 
for /f "delims=" %%i in ('dir /b ^|find "."') do call :sub1 
set /a "rdNum=(%rdm%*%counter%/32767)+1" 
set /a "counter=0" 
for /f "delims=" %%i in ('dir /b ^|find "."') do set "fileName=%%i" &call :sub2 
popd "%workDir%" 
goto :eof 
:sub1 
set /a "counter+=1" 
goto :eof 
:sub2 
set /a "counter+=1" 
if %counter%==%rdNum% (
xcopy /y "C:\Generator\Tickets\"%fileName%"" "C:\Generator\temp" 
) 
goto :eof 

:sub3 
pushd "C:\Generator\temp" && (
     for /f "tokens=1,*" %%j in (' 
      robocopy . . /l /nocopy /is /e /nfl /njh /njs 
     ') do (if %%j neq 10 goto main) 
     popd 
    ) 
goto :eof 
+1

このスレッドに興味がありますか:[純バッチスクリプトで重複のない乱数のリストを生成するには?](http://stackoverflow.com/q/34406594) – aschipfl

答えて

1

である:

@echo off 
set "source=C:\Generator\Tickets" 
set "dest=C:\Generator\temp" 
REM make sure, destination is empty: 
del /q "%dest%\*" 
REM get number of files in source: 
for /f %%a in ('dir /b /a-d "%source%\" ^|find /c /v ""') do set count=%%a 
REM do ten times: 
for /l %%a in (1,1,10) do call :sub 
goto :eof 

:sub 
    REM get file index to copy [see 'set /?` for Modulo-operator]: 
    set /a x=%random% %% %count% +1 
    REM get filename of index: 
    for /f "tokens=* skip=%x%" %%a in ('echo x^&dir /b /a-d') do set "file=%%a" & goto :cont 
    :cont 
    REM if file already exists in destination, try again: 
    REM ATTENTION: this is an endless loop, if there are not enough files in source... 
    if exist "%dest%\%file%" goto :sub 
    REM else copy the file: 
    copy "%source%\%file%" "%dest%\" 
goto :eof 
関連する問題