美好的一天!因此,由于系统的严格限制,我必须在Batch中完成这项任务。
它的原理,随机地"从文件夹中选择文件,并将它们复制到另一个文件夹。 问题是,有时候"随机性"选择相同的文件,因此在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
答案 0 :(得分: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