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-06-22 17:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] Ask about the usage of the random variable View 5,694 Replies 25
Original Poster Posted 2007-01-24 16:54 ·  中国 广东 广州 海珠区 电信
初级用户
Credits 68
Posts 32
Joined 2007-01-24 04:00
19-year member
UID 77490
Gender Male
From 广东潮州
Status Offline
Today I saw too many excellent works here and am so excited that I can't sleep. Suddenly, I thought of a question. It's about the usage of the RANDOM variable. I searched online but couldn't find it, and G (Google) didn't find it either. I thought it shouldn't be a very naive question, so I dare to ask the seniors here. Thank you first.....

First, let's talk about my understanding of "random". It is a variable in WINDOWS, and in DOS, it can be called in the form of %random% like calling %USERNAME%, which can list random decimal numbers between 0 and 32767, right?

Question 1: There are TXT files with filenames from 1 to 20 (1.TXT to 20.TXT) in a folder. How to write a batch file to randomly call one of these 20 files?

Question 2: If the filenames of the 20 files in this folder are in Chinese (also TXT), how to randomly call one TXT?

First, thank you all for your enthusiastic help.
In the past two days, I found a new problem:
When randomly selecting a file in a certain folder, if this batch file is also in it, it may be selected. How to exclude the batch file itself?
(Assuming there are many types of files in the folder, including other batch files; at the same time, it is stipulated that this batch file must be in it)

Moreover: If there are several other files that also want to be excluded from randomness, how to do it? The filename can be represented by *.*.
How to achieve it? Thank you!

[ Last edited by HAT on 2008-10-27 at 11:29 ]
Floor 2 Posted 2007-01-24 17:43 ·  中国 山东 济南 电信
初级用户
Credits 125
Posts 44
Joined 2007-01-24 15:31
19-year member
UID 77555
Gender Female
Status Offline
### 问题1:
```
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

set /a na=%random%%%20+1 & start !na!.txt
```

### 问题2:
```
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

set /a na=%random%%%20+1,n=0
for /f "tokens=*" %%i in ('dir /b') do (set /a n+=1 & if !na!==!n! start %%i)
```
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
PPdos +1 2007-01-24 18:45
tashaxin +1 2007-01-26 23:19
Floor 3 Posted 2007-01-24 18:15 ·  美国 弗吉尼亚州 华盛顿县 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
Question 1:

for /f %%i in ('set /a %random%%%20+1') do start %%i.txt


Question 2:

@echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir/b') do (
set /a Num+=1
set !Num!=%%i
)
for /f %%i in ('set /a %random%%%20+1') do start !%%i!
pause>nul


[ Last edited by PPdos on 2007-1-24 at 06:08 AM ]
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 4 Posted 2007-01-24 18:20 ·  中国 山东 济南 电信
初级用户
Credits 125
Posts 44
Joined 2007-01-24 15:31
19-year member
UID 77555
Gender Female
Status Offline
PPdos

set /a %random%%%20

Is this a formal grammar? Can you explain it? (Closed) set expression

------------------
When %random%%%20 ==0?????
------------------

[ Last edited by qjbm on 2007-1-24 at 05:26 AM ]
Floor 5 Posted 2007-01-24 18:33 ·  美国 弗吉尼亚州 华盛顿县 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
The upstairs has already posted. I'm sorry, I don't know! 1 Should add
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 6 Posted 2007-01-24 21:57 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
I'll join in the fun. The second question of the楼主 can be solved without using a delay variable.

set/a a=%random%%%20+1
for /f "tokens=1,2 delims=:" %%i in ('dir/b ^|findstr /n .*') do (if "%%i"=="%a%" start " " "%%~fsj")


Make a little change to support file names with spaces

[ Last edited by ccwan on 2007-1-24 at 10:12 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
PPdos +3 2007-01-25 04:24
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 7 Posted 2007-01-25 04:34 ·  中国 广东 广州 海珠区 电信
初级用户
Credits 68
Posts 32
Joined 2007-01-24 04:00
19-year member
UID 77490
Gender Male
From 广东潮州
Status Offline
Ha..Everyone gets up so early?

Thank you! Can you make a little explanation? Or is there a link to the information about the usage of these variables?
Floor 8 Posted 2007-01-25 05:12 ·  中国 广东 广州 海珠区 电信
初级用户
Credits 68
Posts 32
Joined 2007-01-24 04:00
19-year member
UID 77490
Gender Male
From 广东潮州
Status Offline
I have found some related articles. Because I just arrived, the environment is not yet familiar...

Thank you all again...
Floor 9 Posted 2007-01-25 05:41 ·  中国 广东 中山 电信
新手上路
Credits 19
Posts 10
Joined 2005-12-13 01:56
20-year member
UID 47019
Status Offline
I only knew before that this was just a random thing, didn't think about applying it. Now after reading this post, I realize it can be like this.
Floor 10 Posted 2007-01-25 07:24 ·  中国 山东 济南 电信
初级用户
Credits 125
Posts 44
Joined 2007-01-24 15:31
19-year member
UID 77555
Gender Female
Status Offline
6 F ccwan

In question 2, if the description file name is in Chinese, it may not have a label.

How to do in your code

"%%i"=="%a%" ???
Floor 11 Posted 2007-01-25 21:15 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
re qjbm

It seems that brother is not familiar with the findstr command. The command "dir/b ^|findstr /n .*'" passes the files dir出来 to the findstr command. The /n parameter is used to add line numbers to each line, similar to this



That is to say, it is not using the numerical values in the file name, but the separately added line number.
You can enter findstr/? at the command line to view the usage of the findstr command.
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
qjbm +1 2007-01-26 13:13
tashaxin +1 2007-01-26 23:20
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 12 Posted 2007-01-26 13:16 ·  中国 山东 济南 电信
初级用户
Credits 125
Posts 44
Joined 2007-01-24 15:31
19-year member
UID 77555
Gender Female
Status Offline
ccwan

Among three people, there must be someone who can be my teacher
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
PPdos +3 2007-01-26 16:55
Floor 13 Posted 2007-01-28 07:03 ·  中国 广东 广州 番禺区 电信
初级用户
Credits 68
Posts 32
Joined 2007-01-24 04:00
19-year member
UID 77490
Gender Male
From 广东潮州
Status Offline
These two days, a new problem was found:
When randomly selecting a file in a certain folder, if this batch processing is also in it, it may also be selected. How to exclude the batch processing itself?
(Assuming there are many types of files in the folder, including other batch processing files; at the same time, it is stipulated that this batch processing must be in it)
How to achieve it? Thanks!

For the convenience, it has been modified to the original post. Please see the original post, thanks

[ Last edited by g4rr on 2007-1-27 at 08:36 PM ]
Floor 14 Posted 2007-01-28 08:33 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
List file names, not displaying "self" (but assuming the batch file extension is .Bat instead of .CMD).


@echo off



dir/b|findstr /i /v "%~n0.bat"|findstr /n .*

pause





According to the discovery of brother ccwan and the principle analysis and method of brother namejm, the above code will make mistakes when processing file names containing spaces and file names partially同名with spaces. The modification is as follows:



@echo off



dir/b|findstr /i /v /c:"%~n0.bat"|findstr /n .*

pause




Added the /c: parameter to make Findstr understand that strings containing spaces are an indivisible whole~: )


For details, see the 16th floor and 17th floor of the principle~: )

[ Last edited by redtek on 2007-1-28 at 10:19 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2007-01-28 21:34
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 15 Posted 2007-01-28 21:37 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Brother redtek's code is concise and wonderful. Appreciate it~~

After testing, dir/b|findstr /i /v "%~0"|findstr /n .* can also succeed, quite interesting^_^
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Forum Jump: