Windows “随机文件选择”;“验证”;成批
你好!因此,由于严重的系统限制,我必须批量完成这项任务 其原理是,它“随机”从一个文件夹中选择文件,并将其复制到另一个文件夹。 问题是,有时“随机性”会选择相同的文件,因此,在10个需要的文件中,我最终得到了8-9个。 我曾尝试过创建一个“验证”系统,但我似乎无法使其在循环中工作,因此它只执行一次,并且仍然有可能选择相同的文件 对不起,如果代码是一个完整的意大利面条,我不是一个程序员 我有一种感觉,解决方案非常简单,我就是看不到 非常感谢您的任何帮助或建议 先谢谢你 “验证”在“sub3”中Windows “随机文件选择”;“验证”;成批,windows,loops,batch-file,cmd,Windows,Loops,Batch File,Cmd,你好!因此,由于严重的系统限制,我必须批量完成这项任务 其原理是,它“随机”从一个文件夹中选择文件,并将其复制到另一个文件夹。 问题是,有时“随机性”会选择相同的文件,因此,在10个需要的文件中,我最终得到了8-9个。 我曾尝试过创建一个“验证”系统,但我似乎无法使其在循环中工作,因此它只执行一次,并且仍然有可能选择相同的文件 对不起,如果代码是一个完整的意大利面条,我不是一个程序员 我有一种感觉,解决方案非常简单,我就是看不到 非常感谢您的任何帮助或建议 先谢谢你 “验证”在“sub3”中 使
使用不同的逻辑:
@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
您可能对此线程感兴趣:
@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