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-07-29 07:58
中国DOS联盟论坛 » 论坛回收站 » [10QB Bounty] Asking everyone View 9,768 Replies 6
Original Poster Posted 2010-11-18 16:11 ·  中国 山西 长治 联通
新手上路
Credits 10
Posts 10
Joined 2010-01-21 01:30
16-year member
UID 159120
Gender Male
Status Offline
Asking everyone, how to use batch processing to call a random line from a text and define variables respectively?

The text content is like:
——————————
WKS001 1 abcdefg
WKS002 2 abcdefg
WKS003 3 abcdefg
WKS004 4 abcdefg
WKS005 5 abcdefg
WKS006 6 abcdefg
WKS006 6 abcdefg
...
——————————
In this way, the number of lines is not fixed, and each line has 3 segments of characters separated by spaces. It is required to randomly调入a certain line in batch processing, and then define the 3 segments of characters of that line as variables a b c

After debugging, I will recharge 10 Q coins to the author as a reward, thank you everyone

[ Last edited by adslcool on 2010-11-18 at 16:15 ]
Floor 2 Posted 2010-11-18 16:12 ·  中国 山西 长治 联通
新手上路
Credits 10
Posts 10
Joined 2010-01-21 01:30
16-year member
UID 159120
Gender Male
Status Offline
Floor 3 Posted 2010-11-18 17:08 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
This Q coin seems not difficult to get... Is the above bid accepted?
Floor 4 Posted 2010-11-18 18:13 ·  中国 湖南 怀化 电信
初级用户
★★
Credits 66
Posts 53
Joined 2009-10-09 20:51
16-year member
UID 152852
Gender Male
Status Offline

::If the number in the middle of each line is the serial number, you can use Findstr to randomly find it.

@echo off & Title Variable Random Definition
Setlocal EnableDelayedExpansion

::Find out how many valid lines are in the file:
For /f %%a in ('findstr "^WKS" File.txt ') do (set /a N+=1)
Echo There are !N! lines of valid data in File.txt!

::Set random number and use Findstr to find the line with the corresponding number in the middle of the line.
set /a Line=%Random% %% !N! +1
For /f "tokens=1-3" %%a in ('findstr " %Line% " File.txt ') do (
set "One=%%a"
set "Two=%%b"
set "Thr=%%c"
)
echo %One%
echo %Two%
echo %Thr%
Endlocal
Pause


[ Last edited by wewebb on 2010-11-18 at 18:30 ]
Floor 5 Posted 2010-11-18 18:15 ·  中国 湖南 怀化 电信
初级用户
★★
Credits 66
Posts 53
Joined 2009-10-09 20:51
16-year member
UID 152852
Gender Male
Status Offline

::If the number in the middle of each line is not a serial number but an indefinite number.

@echo off & Title Variable Random Definition
Setlocal EnableDelayedExpansion

::Find out how many valid lines are in the file and add line numbers to each line:
(For /f "delims=" %%a in ('findstr "^WKS" File.txt ') do (set /a N+=1 & echo !N! %%a) ) > NewFile.txt

Echo NewFile.txt has !N! lines of valid data!

::Set a random line number and use Findstr to find the corresponding line.
set /a Line=%Random% %% !N! +1

For /f "tokens=2-4" %%a in ('findstr "^%Line% " NewFile.txt ') do (
set "One=%%a"
set "Two=%%b"
set "Thr=%%c"
)
echo %One%
echo %Two%
echo %Thr%
Endlocal
Pause


[ Last edited by wewebb on 2010-11-18 at 18:29 ]
Floor 6 Posted 2010-11-18 18:47 ·  中国 湖南 怀化 电信
初级用户
★★
Credits 66
Posts 53
Joined 2009-10-09 20:51
16-year member
UID 152852
Gender Male
Status Offline
```
::If the file content is all in the same format (no extra information lines), and the numbers in each line are of variable numbers.

@echo off & Title Variable Random Definition
Setlocal EnableDelayedExpansion

::Find out how many lines are in the file
For /f "delims=" %%a in (File.txt) do set /a N+=1

Echo File.txt has!N! lines!

::Set random line number
set /a Line=%Random% %%!N! +1

For /f "tokens=1-3" %%a in (File.txt) do (
set /a num+=1
if!num! ==!Line! (set "One=%%a" & set "Two=%%b" & set "Thr=%%c")
)
echo %One%
echo %Two%
echo %Thr%
Endlocal
Pause
```

[ Last edited by wewebb on 2010-11-18 at 18:49 ]
Floor 7 Posted 2010-11-18 22:23 ·  中国 山西 长治 联通
新手上路
Credits 10
Posts 10
Joined 2010-01-21 01:30
16-year member
UID 159120
Gender Male
Status Offline
Thanks to everyone, I'll test it first, the above expert's
Forum Jump: