简体   繁体   中英

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. 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 .

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.ra c 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?

@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:

  • Line #4: it make a pseudo array in f with n incremented by 1
  • Line #5: it take a random number between 0 and the total count of files called n with the help of: %random% modulo n

In this way, this creates a number of variables automatically according to their position then %random% %% n picks one.

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. 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"

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
)

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

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

Put together those parts to get the final script.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM