[英]Command Prompt Search for files in a directory take the name of one random
Inside a directory c:\\configs
I have files with various extensions including the extension .rac. 在目录c:\\configs
我具有带有各种扩展名的文件,包括扩展名.rac。 I need a script for the Windows command prompt that looks inside c:\\configs
for the names of the files that end with the extension .rac, ignoring other extensions. 我需要Windows命令提示符下的脚本,该脚本在c:\\configs
查找以扩展名.rac结尾的文件名,而忽略其他扩展名。 Then of all the names that end with .rac extension the script must choose a random one and process it with the command c:\\programs\\submit.exe namerandom.rac
. 然后,在所有以.rac扩展名结尾的名称中,脚本必须选择一个随机的名称,并使用命令c:\\programs\\submit.exe namerandom.rac
。
For example, suppose that random .rac file is called mosaic.rac
, then the script executes the command c:\\programs\\submit.exe mosaic.rac
. 例如,假设随机的.rac文件名为mosaic.rac
,那么脚本将执行命令c:\\programs\\submit.exe mosaic.rac
。 Of course the mosaic.ra
c name changes each time the script is runs because it is a random selected from the all the .rac files found. 当然,每次运行脚本时, mosaic.ra
c名称都会更改,因为它是从找到的所有.rac文件中随机选择的名称。
Anyone have an idea in how to do and that can put example code? 任何人都有一个想法,可以放置示例代码?
@echo off
setlocal EnableDelayedExpansion & set n=0
for /f "delims=" %%a in ('dir /b /A-D "*.rac"') do (
set "f=%%a" & set "f[!n!]=!f!" & set /a "n+=1")
set /a c=%random% %% n
echo !f[%c%]!
Explanation: 说明:
f
with n
incremented by 1 第4行:在f
创建一个伪数组,其中n
递增1 n
with the help of: %random%
modulo n
第5行:借助%random%
模n
它在0和称为n
的文件总数之间取一个随机数。 In this way, this creates a number of variables automatically according to their position then %random% %% n
picks one. 这样,这将根据它们的位置自动创建多个变量,然后%random% %% n
选择一个。
You might as well picks some manually like this: 您最好像这样手动选择一些:
echo !f[0]! !f[1]! !f[2]! !f[3]! !f[4]! !f[5]! ...
To accomplish that, you may use the following... 为此,您可以使用以下...
Firstly, to get all .rac
files, use the dir
command. 首先,要获取所有.rac
文件,请使用dir
命令。 The /B
switch specifies to output only a bare file list without any headers nor footers. /B
开关指定仅输出裸文件列表,而没有任何页眉或页脚。 If you want to search the given directory recursively, add the /S
switch: 如果要递归搜索给定目录,请添加/S
开关:
dir /B "C:\configs\*.rac"
Secondly, you need to count the number of returned .rac
files. 其次,您需要计算返回的.rac
文件的数量。 You can use a for /F
loop (parsing the output of dir
/B) together with set /A
for that: 您可以将for /F
循环(解析dir
/ B的输出)与set /A
一起使用:
set /A COUNT=0
for /F "delims=| eol=|" %%L in (
'dir /B "C:\configs\*.rac"'
) do (
set /A COUNT+=1
)
Thirdly, you need to compute a random number in the applicable range. 第三,您需要计算适用范围内的随机数。 The built-in variable RANDOM
retrieves a random number from 0
to 32767
, so we need a little maths. 内置变量RANDOM
检索0
到32767
的随机数,因此我们需要一些数学运算。 The result will be a number from 0
to %COUNT% - 1
: 结果将是0
到%COUNT% - 1
:
set /A RNDNUM=%RANDOM%%%COUNT
Fourthly, you can use another for /F
loop with the skip
option to select a random file (we skip the previously calculated number RNDNUM
of lines). 第四,您可以将另一个for /F
循环的“ skip
选项选择一个随机文件(我们跳过先前计算的行数RNDNUM
)。
The if
statement ensures that no skip
option is provided in case the random number is 0
. if
随机数为0
, if
语句确保不提供任何skip
选项。
The goto
command ensures that only the selected file is passed to submit.exe
. goto
命令确保仅将选定的文件传递给submit.exe
。 If you omitted it, every file after the selection would be passed over to submit.exe
too, one after another: 如果省略它,则选择后的每个文件也将一个接一个地传递到submit.exe
:
if %RNDNUM% gtr 0 (
set SKIP=skip=%RNDNUM%
) else (
set SKIP=
)
for /F "%SKIP% delims=| eol=|" %%L in (
'dir /B "C:\configs\*.rac"'
) do (
start "" /WAIT "submit.exe" "%%~L"
goto :CONTINUE
)
:CONTINUE
Put together those parts to get the final script. 将这些部分放在一起以获得最终脚本。
Type each command and append /?
输入每个命令并附加/?
in command prompt to get the respective help text displayed. 在命令提示符下获取显示的相应帮助文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.