一个随机文件复制到另一个文件夹 [英] Copy a random file to another folder
问题描述
我试图创建一个批处理文件从一个文件夹复制一个随机文件到另一个文件夹。但是当我每次运行下面它复制code相同的文件。没有人知道什么是错的code?
我有不同类型的文件夹中的文件.JPG的.txt的.rtf我不知道,这就是为什么它不工作。
关闭@echo
SETLOCAL EnableDelayedExpansion
CD C:\\用户\\保罗\\桌面\\ 11 \\
设定N = 0
在%% F(*。*)做(
集/ A N + = 1
设置文件[!N!= %% F
)
设置/ A兰特=(N *%随机%)/ 32768 + 1
拷贝!文件[%兰特%]! C:\\用户\\保罗\\桌面\\ 12
您通过在资源管理器中双击运行该批处理文件?当cmd.exe的开始执行其初始化与当前时间的随机种子,因此,如果在一个短的延时执行批处理文件几次,并选择不同的文件的数量是小的(象50),相同的文件将是选择每一次。
您可能会随机在几个方面的随机数;这是最简单的方法之一:
为/ L %%我在(1,1,%时间:〜-1%)并设置!假=随机
将使用%随机%
。
I'm trying to create a batch file to copy a random file from one folder to another folder . but when I run the code below it copies the same file every time . does anyone know what's wrong with the code ? I have different types of files in the folder .jpg .txt .rtf I don't know if that is why it is not working.
@echo off
setlocal EnableDelayedExpansion
cd c:\users\paul\desktop\11\
set n=0
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" c:\users\paul\desktop\12
Are you running this Batch file via a double-click from the explorer? When cmd.exe start execution it initialize the random seed with the current time, so if you execute the Batch file several times in a short lapse, and the number of different files to choose is small (like 50), the same file will be selected every time.
You may "randomize" the random number in several ways; this is one of the simplest methods:
for /L %%i in (1,1,%time:~-1%) do set "dummy=!random!"
Insert this line before the line that uses %random%
.
这篇关于一个随机文件复制到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!