中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-13 03:09
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » How to insert multiple parameters in batch processing with random and sequential requirements, thanks!
Printable Version  4,354 / 16
Floor1 xinshou123 Posted 2006-11-01 08:43
初级用户 Posts 8 Credits 20
I'm a novice, and I want to ask a question:

It's a batch processing parameter passing problem: the format is as follows,

There are 4 TXT files:
A1.TXT
__________________________
AAAAAAAAAA
AAAAAAAA
AAAAAAA
AAAAAAAAA
AAAAAAAAAAAAAAA
.....................


A2.TXT
____________________________
BBBBBBBBB
BBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBB
..................


A3.TXT
____________________________
CCCCCCCCCCC
CCCCC
HHHHHHH
CCCCCCCCCCCCCC
............

A4.TXT
___________________________
DFGGGGGGGGGG
GFHHHH
FGHGFH
...................

I want to achieve the result: the content of A1.TXT starts from the first line and is passed sequentially as , passed until the end of the batch script. At the same time, randomly extract one line from document files A2.TXT; A3.TXT; A4.TXT, respectively as
, . The format is as follows:
CV.EXE

[ Last edited by namejm on 2006-11-2 at 08:55 AM ]
Floor2 namejm Posted 2006-11-01 10:39
荣誉版主 Posts 1,737 Credits 5,226 From 成都
May I can't answer this question. You can provide other topics and I will try my best to help you.
Floor3 9527 Posted 2006-11-02 04:51
银牌会员 Posts 438 Credits 1,185 From 北京
@echo off&setlocal
for /f %%a in ('find /c /v "" ^<a2.txt') do set a=%%a
for /f %%a in ('find /c /v "" ^<a3.txt') do set b=%%a
for /f %%a in ('find /c /v "" ^<a4.txt') do set c=%%a

for /f "delims=" %%x in (a1.txt) do call :shit %%x

goto :eof

:shit
set /a x=%random%%%a+1
for /f "tokens=1* delims=:" %%a in ('findstr /n . a2.txt^|findstr "^%x%:.*$"') do set "h=%%b"
set /a x=%random%%%b+1
for /f "tokens=1* delims=:" %%a in ('findstr /n . a3.txt^|findstr "^%x%:.*$"') do set "i=%%b"
set /a x=%random%%%c+1
for /f "tokens=1* delims=:" %%a in ('findstr /n . a4.txt^|findstr "^%x%:.*$"') do set "j=%%b"
echo %1 %h% %i% %j%
goto :eof

No consideration for special symbols and blank lines in the lines, too lazy to write, a bit tired

[ Last edited by 9527 on 2006-11-3 at 01:27 AM ]
Floor4 xinshou123 Posted 2006-11-02 08:42
初级用户 Posts 8 Credits 20
Hello, moderator:
The number of lines in each of the three files A2.TXT;A3.TXT;A4.TXT is about 20-30 lines.
Thank you!
Floor5 xinshou123 Posted 2006-11-02 08:48
初级用户 Posts 8 Credits 20
Originally posted by 9527 at 2006-11-2 04:51:
@echo off&setlocal
for /f %%a in ('find /c /v "" ^<a2.txt') do set a=%%a
for /f %%a in ('find /c /v "" ^<a3.txt') do set b=%%a
for /f %%a in ('find /c /v "&quo ...


Hello, 9527, I'm very green hand. Can you help write it in detail? Thank you first!
Floor6 namejm Posted 2006-11-02 09:34
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The 'findstr /n . a2.txt^|findstr /b %x% statement is not very safe. Take an extreme example, if a line of content contains all the randomly generated %x%, then this line will always be displayed and the random line will not be displayed (this example is not very appropriate, please see the analysis of 8F and 12F), let me use a more understandable code:


[ Last edited by namejm on 2006-11-3 at 01:31 AM ]
Floor7 youxi01 Posted 2006-11-02 12:40
高级用户 Posts 247 Credits 846 From 湖南==》广东
@echo off & set /a a=1
set files=test1.txt test2.txt test3.txt test4.txt
setlocal enabledelayedexpansion
for %%i in (%files%) do call :Config %%i

set /a a=0
for %%i in (%files%) do set /a a+=1 & call :Pick_up %%i
for %%i in (%var1% %var2% %var3% %var4%) do echo %%i
pause>nul & goto :eof

::Get the number of lines in the text
:Config
for /f %%a in ('find /c /v "" ^<%1') do (set /a num%a%=%%a & set /a a+=1 & goto :eof)

::Use more to achieve the purpose of skipping specified lines (and can skip 0 lines, while skip cannot!)
:Pick_up
set /a skipline=%random% %% !num%a%!
set /a sk=%skipline%+1
for /f "delims=" %%i in ('more +%skipline% %1') do (
set var%a%=%1@第%sk%行@:%%i & goto :eof)

Instructions: Derived from 6F and 3F!
The program has been tested and passed under "most normal conditions" and XP PRO!
Floor8 9527 Posted 2006-11-02 23:01
银牌会员 Posts 438 Credits 1,185 From 北京
Take an extreme example, if a certain line contains all randomly generated %x%???

I don't understand what he means? Is it to be afraid of having the same number in the line or something else?
I should not be affected by using the FINDSTR /B number pattern, /B matches the pattern at the beginning of a line
Floor9 youxi01 Posted 2006-11-02 23:07
高级用户 Posts 247 Credits 846 From 湖南==》广东
That is to say, your code obtains the parameter content based on the line number (corresponding number). For example: Now you want to obtain the content of the 5th line. According to your program, it will look for the line containing 5. The 5th line must have 5, but the content of the 4th line may also contain 5, which may lead to misjudgment.
Floor10 9527 Posted 2006-11-02 23:12
银牌会员 Posts 438 Credits 1,185 From 北京
The person upstairs, I understand, heh heh, then what is the use of /B? What does it mean to start the matching mode? I think you'd better experiment with the FINDSTR command carefully
Floor11 youxi01 Posted 2006-11-02 23:59
高级用户 Posts 247 Credits 846 From 湖南==》广东
Got it, thanks. It seems I need to give a thorough review of the switches for each command!
Floor12 namejm Posted 2006-11-03 00:53
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Originally posted by 9527 at 2006-11-2 23:01:
Take an extreme example. If a certain line contains all %x% generated randomly???

I don't understand what he means. Is it to be afraid of having the same number in the line or something else?
I should not be affected by using the FINDSTR /B pattern of numbers. /B matches the pattern at the beginning of a line

  After thinking carefully, this example I gave is not quite appropriate. However, assuming the extreme situation is like this: when %x% takes 1, 12, 123... continuously, then, findstr /b will always match the line with the line number 12345, right? At this time, it would be difficult to get a random line, wouldn't it? More extremely, assuming the total number of lines is 123456789, and the randomly taken number is always any one of 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 23,......123, 234... , isn't it always matching the line 123456789?

  The above is just speculation, no test.
Floor13 9527 Posted 2006-11-03 01:25
银牌会员 Posts 438 Credits 1,185 From 北京
The 3F code has been updated, sorry to the original poster............
Floor14 namejm Posted 2006-11-03 06:21
荣誉版主 Posts 1,737 Credits 5,226 From 成都
I'm going to jump out again to pick on the loopholes of 9527. I hope 9527 doesn't think I'm always picking on you ^_^

The format of findstr /n . a2.txt^|findstr "^%x%:.*$" is still not entirely satisfactory: assuming the last line is not an empty line, then it will never be able to extract the last character line. This should be a small bug in findstr when looking for the end of the line. Please test the following code:

Please test with test.txt that only has the line "123:456" and test.txt where the last line is an empty line and one line is "123:456" respectively.

[ Last edited by namejm on 2006-11-3 at 06:24 AM ]
Floor15 xinshou123 Posted 2006-11-03 08:20
初级用户 Posts 8 Credits 20
Thanks to the moderator, thanks to 9527, thanks to youxi01.

I have only learned the simplest batch processing. Although I can't fully understand all the codes yet, the codes from you all have already met my requirement results. Thanks!

Thanks to everyone's help!!!
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023