中国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-09-23 16:50
47,812 topics / 349,917 posts / today 0 new / 48,274 members
DOS批处理 & 脚本技术(批处理室) » Question: how to write a random value obtained by RANDOM into a file
Printable Version  3,861 / 9
Floor1 falcon Posted 2006-05-08 13:33
新手上路 Posts 3 Credits 10 From 陕西西安
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 ]
Floor2 bagpipe Posted 2006-05-08 16:52
银牌会员 Posts 425 Credits 1,144 From 北京
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.
Floor3 3742668 Posted 2006-05-08 21:39
荣誉版主 Posts 718 Credits 2,013
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.
Floor4 falcon Posted 2006-05-09 10:29
新手上路 Posts 3 Credits 10 From 陕西西安
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 ]
Floor5 3742668 Posted 2006-05-09 12:21
荣誉版主 Posts 718 Credits 2,013
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.
Floor6 bagpipe Posted 2006-05-09 15:00
银牌会员 Posts 425 Credits 1,144 From 北京
::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.................
Floor7 willsort Posted 2006-05-09 17:35
元老会员 Posts 1,512 Credits 4,432
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:


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

[ Last edited by willsort on 2006-5-11 at 16:06 ]
Floor8 falcon Posted 2006-05-09 19:09
新手上路 Posts 3 Credits 10 From 陕西西安
Thanks, everyone. I've benefited a lot and learned a lot.
Floor9 bagpipe Posted 2006-05-10 08:45
银牌会员 Posts 425 Credits 1,144 From 北京
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....
Floor10 bagpipe Posted 2006-05-10 08:57
银牌会员 Posts 425 Credits 1,144 From 北京
Hehe, brother, you seem to be missing a /A parameter. For checking duplicates, I think using findstr is enough.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023