命令提示搜索目录中的文件,名称随机 [英] Command Prompt Search for files in a directory take the name of one random

查看:292
本文介绍了命令提示搜索目录中的文件,名称随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在目录中 c:\configs 我有一些包含各种扩展名的文件,包括扩展名.rac。我需要一个Windows命令提示符的脚本,它在 c:\configs 中查找以扩展名.rac结尾的文件的名称,忽略其他扩展名。然后在以.rac结尾的所有名称中,脚本必须选择一个随机的名称,并使用 c:\programs\submit.exe namerandom.rac

Inside a directory c:\configs I have files with various extensions including the extension .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. 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文件名为 mosaic.rac ,则脚本将执行命令 c:\programs\submit.exe mosaic.rac 。当然,每次运行脚本时, mosaic.ra c名称都会更改,因为它是从找到的所有.rac文件中随机选择的。

For example, suppose that random .rac file is called mosaic.rac, then the script executes the command c:\programs\submit.exe mosaic.rac. Of course the mosaic.rac name changes each time the script is runs because it is a random selected from the all the .rac files found.

任何人都知道如何做,并且可以放置示例代码。

Anyone have an idea in how to do and that can put example code?

推荐答案

您可以使用以下...

To accomplish that, you may use the following...

首先,要获取所有 .rac 文件, code> dir 命令。 / B 开关指定只输出一个没有任何页眉和页脚的裸文件列表。如果要递归搜索给定目录,请添加 / S 开关:

Firstly, to get all .rac files, use the dir command. The /B switch specifies to output only a bare file list without any headers nor footers. If you want to search the given directory recursively, add the /S switch:

dir /B "C:\configs\*.rac"

需要计算返回的 .rac 文件的数量。您可以使用 for / F 循环(解析 dir / B)的输出以及 set / A

Secondly, you need to count the number of returned .rac files. You can use a for /F loop (parsing the output of dir /B) together with set /A for that:

set /A COUNT=0
for /F "delims=| eol=|" %%L in (
  'dir /B "C:\configs\*.rac"'
) do (
  set /A COUNT+=1
)

第三,您需要计算适用范围内的随机数。内置变量 RANDOM 0 检索一个随机数到 32767 ,所以我们需要一个数学。结果将是 0 %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. The result will be a number from 0 to %COUNT% - 1:

set /A RNDNUM=%RANDOM%%%COUNT

第四,您可以使用另一个 for / F 循环与 skip 选项来选择随机文件(我们跳过之前计算的数字 RNDNUM 行)。

如果如果随机数 0 ,则提供跳过选项。

goto 命令确保只将选定的文件传递给 submit.exe 。如果省略它,选择后的每个文件都会一个接一个地传递给 submit.exe

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).
The if statement ensures that no skip option is provided in case the random number is 0.
The goto command ensures that only the selected file is passed to submit.exe. If you omitted it, every file after the selection would be passed over to submit.exe too, one after another:

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

输入每个命令,并在命令提示符中附加 /?相应的帮助文本显示。

Type each command and append /? in command prompt to get the respective help text displayed.

这篇关于命令提示搜索目录中的文件,名称随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆