![]() |
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-15 12:37 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » [Closed] Ask about the usage of the random variable |
| Printable Version 6,152 / 25 |
| Floor1 g4rr | Posted 2007-01-24 16:54 |
| 初级用户 Posts 32 Credits 68 From 广东潮州 | |
|
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 ] |
|
| Floor2 qjbm | Posted 2007-01-24 17:43 |
| 初级用户 Posts 44 Credits 125 | |
|
### 问题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) ``` |
|
| Floor3 PPdos | Posted 2007-01-24 18:15 |
| 高级用户 Posts 268 Credits 783 | |
|
Question 1:
Question 2: [ Last edited by PPdos on 2007-1-24 at 06:08 AM ] |
|
| Floor4 qjbm | Posted 2007-01-24 18:20 |
| 初级用户 Posts 44 Credits 125 | |
|
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 ] |
|
| Floor5 PPdos | Posted 2007-01-24 18:33 |
| 高级用户 Posts 268 Credits 783 | |
|
The upstairs has already posted. I'm sorry, I don't know! 1 Should add
|
|
| Floor6 ccwan | Posted 2007-01-24 21:57 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
I'll join in the fun. The second question of the楼主 can be solved without using a delay variable.
Make a little change to support file names with spaces [ Last edited by ccwan on 2007-1-24 at 10:12 PM ] |
|
| Floor7 g4rr | Posted 2007-01-25 04:34 |
| 初级用户 Posts 32 Credits 68 From 广东潮州 | |
|
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? |
|
| Floor8 g4rr | Posted 2007-01-25 05:12 |
| 初级用户 Posts 32 Credits 68 From 广东潮州 | |
|
I have found some related articles. Because I just arrived, the environment is not yet familiar...
Thank you all again... |
|
| Floor9 lzmyst | Posted 2007-01-25 05:41 |
| 新手上路 Posts 10 Credits 19 | |
|
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.
|
|
| Floor10 qjbm | Posted 2007-01-25 07:24 |
| 初级用户 Posts 44 Credits 125 | |
|
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%" ??? |
|
| Floor11 ccwan | Posted 2007-01-25 21:15 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
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. |
|
| Floor12 qjbm | Posted 2007-01-26 13:16 |
| 初级用户 Posts 44 Credits 125 | |
|
ccwan
Among three people, there must be someone who can be my teacher ![]() |
|
| Floor13 g4rr | Posted 2007-01-28 07:03 |
| 初级用户 Posts 32 Credits 68 From 广东潮州 | |
|
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 ] |
|
| Floor14 redtek | Posted 2007-01-28 08:33 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
List file names, not displaying "self" (but assuming the batch file extension is .Bat instead of .CMD).
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: 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 ] |
|
| Floor15 ccwan | Posted 2007-01-28 21:37 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
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^_^ |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |