中国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-08 02:28
48,041 topics / 350,128 posts / today 3 new / 48,253 members
DOS批处理 & 脚本技术(批处理室) » [Online Help] About using FOR to extract a random line from a TXT file
Printable Version  917 / 9
Floor1 414893029 Posted 2008-04-10 10:46
初级用户 Posts 23 Credits 45
Sorry... although I've been lurking all along... I'm still very much a newbie..

I know the rules... I already searched and still couldn't find what I wanted.. so that's why I opened this thread

Here's the situation... I'm an Internet café administrator.

I just installed Meteor, Butterfly, and a Blade ... because it needs a serial number... I made a registry import...

But... once connected to the network it says the serial numbers are the same...

Mm..

I tried writing a P

@echo off
regedit /s G:\Meteor\流星蝴蝶剑.reg

........
........
reg add "HKEY_LOCAL_MACHINE\Software\InterServ\Meteor" /v Serial /t reg_sz /d %random% /f
start G:\Meteor\Meteor.exe

=======================
流星蝴蝶剑.reg

REGEDIT4


"Path"="G:\\Meteor"
"Exe"="G:\\Meteor\\meteor.exe"
"Serial"="d32c1542240dc7"
"Uninstall"="RunDll32 C:\\PROGRA~1\\COMMON~1\\INSTAL~1\\ENGINE\\6\\INTEL3~1\\Ctor.dll,LaunchSetup \"C:\\Program Files\\InstallShield Installation Information\\{4AB3FF26-7194-49B8-8E3F-C12890B3894C}\\SETUP.EXE\" -l0x804 "


======================

Then.. the problem is that %random% in the P refers to a file of mine called CDK.TXT
======================
CDK.TXT

d32c1542240dc7
f0a03ce05646f4
95d918e3d1ef61
19654494997df4
c3bd21990b3182
72d1e4a2c9c57b
e831d060c0b8a9
43e4a0bb1b4842
abca5be7b61c56
1c7fc8cbc22a2f
862cf5bd2fce26
=======================

My skill level is limited.. I really can't figure out random variables thoroughly...

Now I want %random% to replace a random line from CDK.TXT

to complete the P above


I'd appreciate it if you guys could patiently help me out with this

[ Last edited by 414893029 on 2008-4-10 at 10:48 AM ]
Floor2 terse Posted 2008-04-10 11:16
银牌会员 Posts 946 Credits 2,404
You mean this?
Floor3 414893029 Posted 2008-04-10 11:19
初级用户 Posts 23 Credits 45
Sorry... I really can't understand this....

But... I already made my meaning very clear.. I just want to randomly take one from all those CDKs and write it into the registry
Floor4 slore Posted 2008-04-10 11:22
铂金会员 Posts 2,478 Credits 5,212
Generate a random number, then skip that many lines and read one line...
Floor5 414893029 Posted 2008-04-10 11:29
初级用户 Posts 23 Credits 45
Thanks to terse in reply #2... it can already complete my P..
@echo off&setlocal enabledelayedexpansion
regedit /s G:\Meteor\流星蝴蝶剑.reg
set/a n=%random%%%11+1
for /f "delims=" %%i in (CDK.TXT) do (
set /a m+=1
if !m! equ %n% set "str=%%i"
)
echo %str%
reg add "HKEY_LOCAL_MACHINE\Software\InterServ\Meteor" /v Serial /t reg_sz /d %str% /f
start G:\Meteor\Meteor.exe
Floor6 gao123 Posted 2008-04-10 13:37
新手上路 Posts 6 Credits 12
Floor7 gao123 Posted 2008-04-10 13:38
新手上路 Posts 6 Credits 12
Learning
Floor8 414893029 Posted 2008-04-10 17:04
初级用户 Posts 23 Credits 45
Actually I still don't really understand it.... hope someone can kindly explain the details
Floor9 abcd Posted 2008-04-10 17:14
银牌会员 Posts 739 Credits 1,436
set/a n=%random%%%11+1

In that, %random%%%11 will produce a random number from 0 to 10. Adding +1 then produces a random number from 1 to 11.

The rest is just taking the line from the file that matches that random number.
Floor10 bat-zw Posted 2008-04-10 17:27
金牌会员 Posts 1,276 Credits 3,105
@echo off&setlocal enabledelayedexpansion
set/a n=%random%%%11+1
for /f "delims=" %%i in (CDK.TXT) do (
set /a m+=1
if !m! equ %n% set "str=%%i"
)
echo %str%
pause

Following the OP's meaning, here's a line-by-line explanation:
@echo off&setlocal enabledelayedexpansion turns off all command echoing (including echo itself), then enables delayed variable expansion. Note that delayed expansion is enabled to prepare for the step-by-step assignment of variable m later.
set/a n=%random%%%11+1 gets a random value between 1 and 11 (because cdk.txt has lines 1 through 11). Note that set/a n=%random%%%11 gets a random value between 0 and 10, and the +1 afterward shifts that range to 1 through 11.
for /f "delims=" %%i in (CDK.TXT) do ( set /a m+=1 if !m! equ %n% set "str=%%i") uses the for command to compare against all lines in cdk.txt. Each time a line is processed, the assigned value of variable m is increased by 1. When m accumulates to the same value as the previously obtained n, the line just processed is assigned to variable str. In other words, it randomly takes one line from cdk.txt and assigns it to str. Note that here %m% must be written as !m!, otherwise m will always be empty.
That's about all for this rough explanation. Hope it helps the OP.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023