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-08-08 01:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Online Help] About using FOR to extract a random line from a TXT file View 916 Replies 9
Original Poster Posted 2008-04-10 10:46 ·  中国 上海 杨浦区 电信
初级用户
Credits 45
Posts 23
Joined 2007-10-29 10:36
18-year member
UID 101072
Gender Male
Status Offline
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 ]
Floor 2 Posted 2008-04-10 11:16 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
You mean this?

@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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
414893029 +2 2008-04-10 11:29
简单!简单!再简单!
Floor 3 Posted 2008-04-10 11:19 ·  中国 上海 杨浦区 电信
初级用户
Credits 45
Posts 23
Joined 2007-10-29 10:36
18-year member
UID 101072
Gender Male
Status Offline
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
Floor 4 Posted 2008-04-10 11:22 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Generate a random number, then skip that many lines and read one line...
Floor 5 Posted 2008-04-10 11:29 ·  中国 上海 杨浦区 电信
初级用户
Credits 45
Posts 23
Joined 2007-10-29 10:36
18-year member
UID 101072
Gender Male
Status Offline
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
Floor 6 Posted 2008-04-10 13:37 ·  日本
新手上路
Credits 12
Posts 6
Joined 2008-02-21 22:25
18-year member
UID 111169
Gender Male
Status Offline
Floor 7 Posted 2008-04-10 13:38 ·  日本
新手上路
Credits 12
Posts 6
Joined 2008-02-21 22:25
18-year member
UID 111169
Gender Male
Status Offline
Learning
Floor 8 Posted 2008-04-10 17:04 ·  中国 上海 杨浦区 电信
初级用户
Credits 45
Posts 23
Joined 2007-10-29 10:36
18-year member
UID 101072
Gender Male
Status Offline
Actually I still don't really understand it.... hope someone can kindly explain the details
Floor 9 Posted 2008-04-10 17:14 ·  中国 北京 华为云
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
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.
Floor 10 Posted 2008-04-10 17:27 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
@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.
批处理之家新域名:www.bathome.net
Forum Jump: