China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-27 14:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to insert multiple parameters in batch processing with random and sequential requirements, thanks! View 4,141 Replies 16
Original Poster Posted 2006-11-01 08:43 ·  中国 山东 烟台 联通
初级用户
Credits 20
Posts 8
Joined 2006-08-30 00:02
19-year member
UID 61548
Status Offline
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 ]
Floor 2 Posted 2006-11-01 10:39 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
May I can't answer this question. You can provide other topics and I will try my best to help you.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-11-02 04:51 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
@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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
xinshou123 +2 2006-11-03 08:09
我今后在论坛的目标就是做个超级坏人!!!
Floor 4 Posted 2006-11-02 08:42 ·  中国 山东 烟台 联通
初级用户
Credits 20
Posts 8
Joined 2006-08-30 00:02
19-year member
UID 61548
Status Offline
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!
Floor 5 Posted 2006-11-02 08:48 ·  中国 山东 烟台 联通
初级用户
Credits 20
Posts 8
Joined 2006-08-30 00:02
19-year member
UID 61548
Status Offline
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!
Floor 6 Posted 2006-11-02 09:34 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:

@echo off
:: Suppose the number of lines of A2.TXT, A3.TXT, A4.TXT are 2, 3, 4 respectively
setlocal enabledelayedexpansion
for /f "delims=" %%i in (A1.TXT) do (
set var1=%%i
call :pick-up2
echo !var1! !var2! !var3! !var4!
)
pause
goto :eof

:pick-up2
set /a num2=2-(1%random:~-1%%random:~0,1%-100)
if %num2% leq 0 goto pick-up2
for /f "tokens=1* delims=:" %%i in ('findstr /n . A2.TXT') do if %%i equ %num2% set var2=%%j

:pick-up3
set /a num3=3-(1%random:~-1%%random:~0,1%-100)
if %num3% leq 0 goto pick-up3
for /f "tokens=1* delims=:" %%i in ('findstr /n . A3.TXT') do if %%i equ %num3% set var3=%%j

:pick-up4
set /a num4=4-(1%random:~-1%%random:~0,1%-100)
if %num4% leq 0 goto pick-up4
for /f "tokens=1* delims=:" %%i in ('findstr /n . A4.TXT') do if %%i equ %num4% set var4=%%j
goto :eof


[ Last edited by namejm on 2006-11-3 at 01:31 AM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-11-02 12:40 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
@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!
Floor 8 Posted 2006-11-02 23:01 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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
我今后在论坛的目标就是做个超级坏人!!!
Floor 9 Posted 2006-11-02 23:07 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
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.
Floor 10 Posted 2006-11-02 23:12 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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
我今后在论坛的目标就是做个超级坏人!!!
Floor 11 Posted 2006-11-02 23:59 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Got it, thanks. It seems I need to give a thorough review of the switches for each command!
Floor 12 Posted 2006-11-03 00:53 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 13 Posted 2006-11-03 01:25 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
The 3F code has been updated, sorry to the original poster............
我今后在论坛的目标就是做个超级坏人!!!
Floor 14 Posted 2006-11-03 06:21 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:

@echo off
findstr "^123:.*$" test.txt>nul && echo yes||echo no
pause

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 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 15 Posted 2006-11-03 08:20 ·  中国 山东 烟台 联通
初级用户
Credits 20
Posts 8
Joined 2006-08-30 00:02
19-year member
UID 61548
Status Offline
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!!!
Forum Jump: