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 14:14
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] Ask about the usage of the random variable View 5,690 Replies 25
Floor 16 Posted 2007-01-28 21:45 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Hehe, a new discovery: There are two files on my desktop, "New Batch.bat" and "New Batch(2).bat". The code of the former is

@echo off



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

pause


The code of the latter is

@echo off



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

pause


The result of running the former is:


The result of running the latter is:


It's like this: The former can recognize the latter, but the latter can't recognize the former. Interesting, right
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2007-01-28 23:12
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 17 Posted 2007-01-28 23:08 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Actually, this is an important feature of the `findstr "string1 + space + string2 + space + string3 + space……"` statement, that is: as long as any string separated by spaces in the quotes is found, findstr considers that a matching string is found. For example, the following code:


@echo off
findstr "abc 123 xyz" test.txt
pause


Test the content of test.txt with abc, 123, xyz or their combinations like abc123, 123xyz, etc., and you will find that the execution result of the above code always displays the content of test.txt.

If you want to find the string "abc 123 xyz" (excluding the quotes at the front and back), you need to add the `/c:` switch, and it becomes `findstr /c:"abc 123 xyz" test.txt`.

[ Last edited by namejm on 2007-1-28 at 10:27 AM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
redtek +5 2007-01-28 23:12
ccwan +5 2007-01-28 23:15
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 18 Posted 2007-01-28 23:11 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Haha... This is fun~ :)

After turning on @echo on, it was found to be related to Findstr:

Run the following code (file name: "New Batch(2).bat"):


@echo on
::off

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

pause


When @echo on is enabled and the code is run:


dir/b | findstr /i /v "New Batch(2).bat" | findstr /n .*
 ……
 ……



It was found that the file name "New Batch(2).bat" searched by Findstr above is a file name with spaces in the middle, and this file name with spaces is also the string to be searched by Findstr.

However, the characteristic of Findstr is that "string A string B" Although they are within the same quote range, the characteristic of Findstr is that it will be understood to search for both conditions at the same time, that is, as long as it contains "string A" or contains "string B", then it meets the target that Findstr wants to search.

And the file name to be searched in the above quotes happens to have spaces, so Findstr understands it as: find "New" or the string containing "Batch(2).bat" meets the condition, plus /V to filter out the target, and the file names of these two batches both contain The retrieval success condition of "New" is filtered out.

After renaming the file name and removing the spaces in the middle, the search is correct, and the above factor is inferred~ :)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2007-01-28 23:15
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 19 Posted 2007-01-28 23:23 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Thanks to two's wonderful explanations. It seems that to make the command execute successfully, we should ensure that the batch file name contains as few spaces as possible.

[ Last edited by ccwan on 2007-1-28 at 11:24 PM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 20 Posted 2007-01-28 23:29 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Names with spaces are also okay. The solutions are:
1. As I said on floor 17, you need to add the /c: switch to findstr;
2. Replace findstr with find.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 21 Posted 2007-01-29 06:37 ·  中国 广东 广州 电信
初级用户
Credits 68
Posts 32
Joined 2007-01-24 04:00
19-year member
UID 77490
Gender Male
From 广东潮州
Status Offline
Originally posted by ccwan at 2007-1-24 08:57 AM:

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


Why is there a ^?
In batch processing, you have to use ^, but not in the command prompt. Can you provide a link? Brother ccwan
I can't find the relevant explanation

[ Last edited by g4rr on 2007-1-28 at 05:40 PM ]
Floor 22 Posted 2007-01-29 06:50 ·  中国 广东 广州 电信
初级用户
Credits 68
Posts 32
Joined 2007-01-24 04:00
19-year member
UID 77490
Gender Male
From 广东潮州
Status Offline
namejm's explanation is really good. Got it! Thanks

I can sense everyone's fascination with batch processing... They can discuss like this for a little problem. I like this atmosphere more and more.
Unfortunately, for now I can only be a bystander to feel your passion and get excited. But I can't participate in the discussion yet.
I will work hard to become one of you..
Thanks you, ccwan, namejm, redtek

[ Last edited by g4rr on 2007-1-28 at 05:54 PM ]
Floor 23 Posted 2007-03-14 22:50 ·  中国 江苏 南京 电信
初级用户
Credits 76
Posts 39
Joined 2007-03-09 06:54
19-year member
UID 81169
Gender Male
Status Offline
Originally posted by g4rr at 2007-1-28 17:50:
namejm's explanation is really good. I understand! Thank you

I can feel everyone's fascination with batch processing... We can discuss such small issues like this. I like this atmosphere more and more.
Unfortunately, for now I only ...

The same as above.................................
狐狸喜欢狡猾。。
Floor 24 Posted 2007-03-15 11:37 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
Leave a mark, will study later...
Floor 25 Posted 2007-04-04 01:27 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
set/a a=%random%%%20+1
for /f "tokens=1,2 delims=: %%i in ('dir/b ^|findstr /n .*') do (if "%%i"=="%a%" start %%~fsj)

Does variable delay not need?
Already understood. Thank you lxmxn

[ Last edited by wudixin96 on 2007-4-4 at 01:49 AM ]
Floor 26 Posted 2009-02-17 20:05 ·  中国 广东 东莞 电信
新手上路
Credits 1
Posts 1
Joined 2009-02-16 18:06
17-year member
UID 139007
Gender Male
From 携享家园
Status Offline
Haha, not bad! All experts
Forum Jump: