|
xinshou123
初级用户
 
积分 20
发帖 8
注册 2006-8-30
状态 离线
|
『楼 主』:
如何在批处理中插入多个参数,并有随机和顺序的要求,谢谢!
使用 LLM 解释/回答一下
在下菜鸟新手,想请教个问题:
是个批处理参数传递问题:格式如下,
有4个TXT文件:
A1.TXT
__________________________
AAAAAAAAAA
AAAAAAAA
AAAAAAA
AAAAAAAAA
AAAAAAAAAAAAAAA
.....................
A2.TXT
____________________________
BBBBBBBBB
BBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBB
..................
A3.TXT
____________________________
CCCCCCCCCCC
CCCCC
HHHHHHH
CCCCCCCCCCCCCC
............
A4.TXT
___________________________
DFGGGGGGGGGG
GFHHHH
FGHGFH
...................
我想实现的结果是:A1.TXT的内容从第一行开始顺序传递作为,传递到最后一行批处理脚本结束,同时在文挡文件A2.TXT;A3.TXT;A4.TXT中各随机提取一行,分别作为
,.格式如下:
CV.EXE
Last edited by namejm on 2006-11-2 at 08:55 AM ]
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 ]
|
|
2006-11-1 08:43 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
请问A2.TXT;A3.TXT;A4.TXT这三个文件中,每个文件的行数在一个什么范围内?
May I can't answer this question. You can provide other topics and I will try my best to help you.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-1 10:39 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
@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
没有考虑行中的特殊符号还有空行,懒得写,有点累了
Last edited by 9527 on 2006-11-3 at 01:27 AM ]
@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 ]
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2006-11-2 04:51 |
|
|
xinshou123
初级用户
 
积分 20
发帖 8
注册 2006-8-30
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
你好,版主:
A2.TXT;A3.TXT;A4.TXT这三个文件中,每个文件的行数在20-30行左右
麻烦您了!
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!
|
|
2006-11-2 08:42 |
|
|
xinshou123
初级用户
 
积分 20
发帖 8
注册 2006-8-30
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
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 ...
你好,9527,本人很菜鸟,能不能帮忙写详细点,先谢谢了!!
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!
|
|
2006-11-2 08:48 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
3F的 'findstr /n . a2.txt^|findstr /b %x% 语句不是十分保险,举个极端的例子,如果某行内容含有随机生成的所有%x%的话,则始终会显示这一行,而不会显示随机的行 (这个例子举得不太妥当,请看8F和12F的分析),我来个容易看懂点的代码:
@echo off
:: 假设A2.TXT、A3.TXT、A4.TXT的行数分别为2、3、4
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 ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-2 09:34 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
@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
::获取文本的行数
:Config
for /f %%a in ('find /c /v "" ^<%1') do (set /a num%a%=%%a & set /a a+=1 & goto :eof)
::利用more达到跳过指定行的目的(而且可以跳过0行,而使用skip则不行!)
: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)
说明:在6F、3F的基础上得来!
程序在“最正常情况”、XP PRO下测试通过!
@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!
|
|
2006-11-2 12:40 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
举个极端的例子,如果某行内容含有随机生成的所有%x%的话???
我不明白他说的是什么意思?是怕行里有相同的数字还是什么其他的东西?
我用FINDSTR /B 数字的模式应该不会影响到吧,/B 在一行的开始匹配模式
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
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2006-11-2 23:01 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
也就是说你的代码是根据行号(对应的数字)来获取参数内容的。打个比方:
你现在要获取第5行的内容,根据你的程序,就会查找含有5的行,第5行肯定有5,但是在第四行的内容也可能含有5,这样的话可能导致误判啊。
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.
|
|
2006-11-2 23:07 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
楼上的,我明白,呵呵,那/B干什么用的啊?什么叫开始匹配模式啊?我想你还是好好实验一下FINDSTR命令吧
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
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2006-11-2 23:12 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
收到,谢了。看样子对各命令的开关,要狠狠补补课才行啊!
Got it, thanks. It seems I need to give a thorough review of the switches for each command!
|
|
2006-11-2 23:59 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Originally posted by 9527 at 2006-11-2 23:01:
举个极端的例子,如果某行内容含有随机生成的所有%x%的话???
我不明白他说的是什么意思?是怕行里有相同的数字还是什么其他的东西?
我用FINDSTR /B 数字的模式应该不会影响到吧,/B 在一行的开始匹配模式
仔细想了一下,我所举的这个例子不太妥当,但是,假设极端的情况是这样的:当 %x% 分别连续取到 1、12、123……的时候,那么,findstr /b 始终会匹配到行数为 12345 这样的行吧?这个时候岂不是难以取到随机的行了吗?更极端一点,假设总行数为 123456789,而随机取到的数字一直是 1、2、3、4、5、6、7、8、9、12、23、……123、234…… 中的任意一个,岂不是始终匹配第 123456789 行了呢?
以上仅属推测,没有测试。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-3 00:53 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
3F代码已经更新,对不住楼主了............
The 3F code has been updated, sorry to the original poster............
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2006-11-3 01:25 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
我又要跳出来挑9527的漏洞了,希望9527别觉得我老是和你过不去^_^
findstr /n . a2.txt^|findstr "^%x%:.*$ 的格式还是难以尽如人意:假设最后一行不是空行,那么,它将永远无法提取最后一个字符行,这应该是 findstr 在查找行尾时的一个小bug。请测试以下代码:
@echo off
findstr "^123:.*$" test.txt>nul && echo yes||echo no
pause
请分别用只有 123:456 这一行内容的test.txt和最后一行为空行、其中一行为 123:456 的test.txt来测试。
Last edited by namejm on 2006-11-3 at 06:24 AM ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-3 06:21 |
|
|
xinshou123
初级用户
 
积分 20
发帖 8
注册 2006-8-30
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
谢谢版主,谢谢9527,谢谢youxi01
我只学过最简单的批处理,虽然还不能完全看懂所有代码,但各位代码已经能满足我的要求结果,谢谢!
谢谢大家的帮助!!!
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!!!
|
|
2006-11-3 08:20 |
|