|
g4rr
初级用户
 
积分 68
发帖 32
注册 2007-1-24 来自 广东潮州
状态 离线
|
『楼 主』:
[已结]请教关于random变量的用法
使用 LLM 解释/回答一下
今天在这里看了太多精品,兴奋得睡不着觉.突然想到一个问题.是关于RANDOM变量的用法的.上网百了一下找不到,G了一下也没找到.想想应该不是很幼稚的问题,所以才敢再请教各位前辈们.先谢谢啦.....
首先,说一下自己对"random"的理解.他是WINDOWS中的一个变量,在DOS下可用
%random%的形式像调用%USERNAME%一样调用它,可列出界于0 和 32767之间的随机十进制数,是吗?
问题1: 一文件夹里面有1~20为文件名的TXT文件(1.TXT到20.TXT),我想在这20个文件中随机调用一个该怎么写批处理?
问题2: 如果这个文件夹中的20个文件的文件名是中文的(同样是TXT),又该如何随机调用一个TXT?
首先,谢谢大家的热心帮忙.
这两天发现一个新问题:
在某一文件夹中随机选择一个文件的时候,如果此批处理也在其中,也有可能被选中,如何把批处理自身排除在外?
(假设文件夹中有很多类型的文件,包括其他批处理文件; 同时,规定此批处理一定要在其中)
再有:如果有其他几个文件也想排除在随机之外,怎么做?(文件名可用*.*代替)
怎么实现呢?谢谢!
Last edited by HAT on 2008-10-27 at 11:29 ]
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 ]
|
|
2007-1-24 16:54 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
 『第 2 楼』:
使用 LLM 解释/回答一下
问题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)
### 问题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)
```
此帖被 +2 点积分 点击查看详情 评分人:【 PPdos 】 | 分数: +1 | 时间:2007-1-24 18:45 | 评分人:【 tashaxin 】 | 分数: +1 | 时间:2007-1-26 23:19 |
|
|
|
2007-1-24 17:43 |
|
|
PPdos
高级用户
   
积分 783
发帖 268
注册 2006-12-26
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
问题1:
for /f %%i in ('set /a %random%%%20+1') do start %%i.txt
问题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 ]
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 ]
|

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃. |
|
2007-1-24 18:15 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
PPdos
set /a %random%%%20
是正规语法吗??能解释一下吗?(已结) set expression
------------------
%random%%%20 ==0 时?????
------------------
Last edited by qjbm on 2007-1-24 at 05:26 AM ]
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 ]
|
|
2007-1-24 18:20 |
|
|
PPdos
高级用户
   
积分 783
发帖 268
注册 2006-12-26
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
楼上已发 恕小弟不知! 1 该加
The upstairs has already posted. I'm sorry, I don't know! 1 Should add
|

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃. |
|
2007-1-24 18:33 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
  『第 6 楼』:
使用 LLM 解释/回答一下
我来凑个热闹,楼主第二个问题可以不用延迟变量解决的。
set/a a=%random%%%20+1
for /f "tokens=1,2 delims=:" %%i in ('dir/b ^|findstr /n .*') do (if "%%i"=="%a%" start %%~fsj)
变动一点,支持带空格的文件名
Last edited by ccwan on 2007-1-24 at 10:12 PM ]
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 ]
此帖被 +3 点积分 点击查看详情 评分人:【 PPdos 】 | 分数: +3 | 时间:2007-1-25 04:24 |
|
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-1-24 21:57 |
|
|
g4rr
初级用户
 
积分 68
发帖 32
注册 2007-1-24 来自 广东潮州
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
哈..大家都这么早起床的啊?
谢谢!可以作一点点说明吗?或者哪里有这些变量用法的资料给个链接好吗?
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?
|
|
2007-1-25 04:34 |
|
|
g4rr
初级用户
 
积分 68
发帖 32
注册 2007-1-24 来自 广东潮州
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
找了一些相关文章了.因为刚来,环境还不熟悉呢...
再一次谢谢各位..
I have found some related articles. Because I just arrived, the environment is not yet familiar...
Thank you all again...
|
|
2007-1-25 05:12 |
|
|
lzmyst
新手上路

积分 19
发帖 10
注册 2005-12-13
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
原来只知道这只是一个随机,没想过应用,现在看了这贴,原来可以这样。
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.
|
|
2007-1-25 05:41 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
6 F ccwan
在问题2 中,描述文件名为中文,则有可能不带标号.
请问在贵代码中如何
"%%i"=="%a%" ???
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%" ???
|
|
2007-1-25 07:24 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
re qjbm
看来兄对findstr命令不熟悉,dir/b ^|findstr /n .*' 命令将dir出来的文件传递给findstr命令,/n参数用来给每一行添加行号,类似这样
也就是说,并非利用文件名内的数值,而是另行添加的行号数字。
你可以在命令行下输入 findstr/? 来查看findstr命令的用法。
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.
此帖被 +2 点积分 点击查看详情 评分人:【 qjbm 】 | 分数: +1 | 时间:2007-1-26 13:13 | 评分人:【 tashaxin 】 | 分数: +1 | 时间:2007-1-26 23:20 |
|
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-1-25 21:15 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
|
2007-1-26 13:16 |
|
|
g4rr
初级用户
 
积分 68
发帖 32
注册 2007-1-24 来自 广东潮州
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
这两天发现一个新问题:
在某一文件夹中随机选择一个文件的时候,如果此批处理也在其中,也有可能被选中,如何把批处理自身排除在外?
(假设文件夹中有很多类型的文件,包括其他批处理文件; 同时,规定此批处理一定要在其中)
怎么实现呢?谢谢!
为了方便,已修改至原贴.请看原帖,谢谢
Last edited by g4rr on 2007-1-27 at 08:36 PM ]
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 ]
|
|
2007-1-28 07:03 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
 『第 14 楼』:
使用 LLM 解释/回答一下
列文件名,不显示 “自己”(但假定批处理的扩展名是 .Bat 而不是 .CMD)。
@echo off
dir/b|findstr /i /v "%~n0.bat"|findstr /n .*
pause
根据ccwan兄的发现与namejm兄的原理剖析和方法,上面代码在处理查找含有空格的文件名与含有空格文件名部分同名的文件时会出错,修改如下:
@echo off
dir/b|findstr /i /v /c:"%~n0.bat"|findstr /n .*
pause
增加了 /c:参数,将含有空格的字符串让 Findstr 理解为它们是一个不可分割的整体~:)
详见原理见第16楼、17楼~:)
Last edited by redtek on 2007-1-28 at 10:19 AM ]
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 ]
此帖被 +5 点积分 点击查看详情 评分人:【 ccwan 】 | 分数: +5 | 时间:2007-1-28 21:34 |
|
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2007-1-28 08:33 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
redtek兄的代码很简洁、精彩。欣赏~~
经过测试,dir/b|findstr /i /v "%~0"|findstr /n .*也能成功,挺有意思^_^
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^_^
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-1-28 21:37 |
|