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-01 15:47
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question: how to write a random value obtained by RANDOM into a file View 3,762 Replies 9
Original Poster Posted 2006-05-08 13:33 ·  中国 陕西 西安 电信
新手上路
Credits 10
Posts 3
Joined 2006-05-08 13:22
20-year member
UID 55144
Gender Male
From 陕西西安
Status Offline
How to write a random value obtained by RANDOM into a specified location in a file

[ Last edited by falcon on 2006-5-8 at 14:08 ]
Floor 2 Posted 2006-05-08 16:52 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
That depends on what kind of file you want to write into. Some things are unclear, you should give more details, for example, which lines to write to, whether there is a fixed position. In short, I can't answer this question.
Floor 3 Posted 2006-05-08 21:39 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
If you want to do it purely with a batch file, the efficiency and fault tolerance are not very ideal.
Briefly, the steps are:
First use for /f "tokens=1,2* delims=:" %%i in ('"findstr /n . file.txt"') do set %%i=%%j & set num=%%i to save the entire file contents into variables named by line number, and save the total number of lines into variable num.
Then modify the content of the specified line as required. You can use set var=%var:~0,number of characters before the modified content%%random%%content after it%
Finally, use for to read the source text content and count, exit the for when it reaches the line before the replaced line, then use echo to write the replaced line into the new file, and then use the skip parameter of for to skip the replaced line and append the remaining content to the new file.
Floor 4 Posted 2006-05-09 10:29 ·  中国 陕西 西安 电信
新手上路
Credits 10
Posts 3
Joined 2006-05-08 13:22
20-year member
UID 55144
Gender Male
From 陕西西安
Status Offline
What I wrote:
@echo off
random 1 255


echo IP=192.168.0.%random%> Wattcp.cfg
echo NETMASK=255.255.255.0>> Wattcp.cfg
echo GATEWAY=192.168.0.1>> Wattcp.cfg

set random=

end


I want to write the resulting number into a specified location in Wattcp.cfg. Can this be done?

I tried the content in post #3, but it wasn't very ideal. Maybe my syntax has a problem?

[ Last edited by falcon on 2006-5-9 at 10:33 ]
Floor 5 Posted 2006-05-09 12:21 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Why not try using vbs to handle it?
Relatively speaking, vbs should be much more efficient, and bat is a bit inadequate when dealing with special characters.
You can refer to this post. The function you want should be similar to what's in there. You only need to make slight changes to the batch script and vbs script in it to achieve what you want.
Floor 6 Posted 2006-05-09 15:00 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
::First case: sequential writing, with blank lines between each section.

@echo off
for /l %%i in (1,1,255) do (
echo IP=192.168.0.%%i>>Wattcp.cfg
echo NETMASK=255.255.255.0>>Wattcp.cfg
echo GATEWAY=192.168.0.1>>Wattcp.cfg
echo.>>Wattcp.cfg
)


::Second case: writing random numbers. Since you're using IP addresses as the basis, there can't possibly be an IP like 192.168.1.263563, right? So we can only make do with the following to implement randomness.
@echo off
setlocal
echo.>Wattcp.cfg
:B
set /a p+=1
if p==5 goto :eof
set ii=%random:~0,3%
if %ii% leq 255 (
for /f "tokens=1,2* delims==" %%i in ('findstr /x "IP=192.168.0.%ii%" Wattcp.cfg') do set uu=%%j
if "%uu%"=="192.168.0.%ii%" goto A
echo IP=192.168.0.%ii%>>Wattcp.cfg
echo NETMASK=255.255.255.0>>Wattcp.cfg
echo GATEWAY=192.168.0.1>>Wattcp.cfg
echo.>>Wattcp.cfg
) else (
goto A
)

:A

set i=%random:~0,2%
set k=%random:~0,1%

for /f "tokens=1,2* delims==" %%i in ('findstr /x "IP=192.168.0.%i%" Wattcp.cfg') do set uu=%%j
if "%uu%"=="192.168.0.%i%" goto B
echo IP=192.168.0.%i%>>Wattcp.cfg
echo NETMASK=255.255.255.0>>Wattcp.cfg
echo GATEWAY=192.168.0.1>>Wattcp.cfg
echo.>>Wattcp.cfg
for /f "tokens=1,2* delims==" %%i in ('findstr /x "IP=192.168.0.%k%" Wattcp.cfg') do set uu=%%j
if "%uu%"=="192.168.0.%k%" goto B
echo IP=192.168.0.%k%>>Wattcp.cfg
echo NETMASK=255.255.255.0>>Wattcp.cfg
echo GATEWAY=192.168.0.1>>Wattcp.cfg
echo.>>Wattcp.cfg

goto B



This is about as far as I can write it in batch.................
Floor 7 Posted 2006-05-09 17:35 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re falcon:

Your problem description is still not clear. I still can't determine how exactly your "specified location" is specified. If it is specified by line number + column number, or line number + feature string, you can refer to the algorithm in post #3. If it is specified by a feature line, you can borrow the approach in post #6.

Also, please state your environment: MSDOS, Win98 command line, or WinXP command line?

Re bagpipe:

I feel your second solution is overly complicated, probably because of the separate handling for random IPs. Under CMD, to get a random IP from 1 to 255 you can use the following code:

set /a rnd=%random% %% 255 + 1


Modified according to brother bagpipe's correction in post #10

[ Last edited by willsort on 2006-5-11 at 16:06 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2006-05-09 19:09 ·  中国 陕西 西安 电信
新手上路
Credits 10
Posts 3
Joined 2006-05-08 13:22
20-year member
UID 55144
Gender Male
From 陕西西安
Status Offline
Thanks, everyone. I've benefited a lot and learned a lot.
Floor 9 Posted 2006-05-10 08:45 ·  中国 北京 顺义区 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Yeah, if brother willsort had come earlier I wouldn't have had to go to so much trouble. It got a bit complicated and overly cumbersome. I've benefited a lot.... Thanks to brother WILLSORT for pointing me in the right direction....
Floor 10 Posted 2006-05-10 08:57 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Hehe, brother, you seem to be missing a /A parameter. For checking duplicates, I think using findstr is enough.
Forum Jump: