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-08-06 02:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » What's wrong with my findstr View 2,972 Replies 22
Original Poster Posted 2006-09-27 01:18 ·  中国 福建 三明 电信
中级用户
★★
Credits 316
Posts 83
Joined 2005-03-01 00:00
21-year member
UID 36626
Gender Male
Status Offline
D:\scripts>for /F %i in ('dir /ad /s /b') do echo %~ni | findstr "123"

D:\scripts>echo 123 | findstr "123"
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
^CEnter your code:Enter your code:Enter your code:Enter your code:Enter your cod
e:Enter your code:Enter your code:Enter your code:^C^CEnter your code:Enter your
code:^C^CERROR!!!
Floor 2 Posted 2006-09-27 01:23 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
There is something wrong with your code. Try the following statement:
for /F %i in ('dir /ad /s /b') do (echo %~ni|findstr "123">nul && echo %~ni)
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-09-27 01:30 ·  中国 福建 三明 电信
中级用户
★★
Credits 316
Posts 83
Joined 2005-03-01 00:00
21-year member
UID 36626
Gender Male
Status Offline
It still doesn't seem to work.

D:\scripts>for /F %i in ('dir /ad /s /b') do (echo %~ni | findstr "123" && echo %~ni )

D:\scripts>(echo 123 | findstr "123" && echo 123 )
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Enter your code:Enter your code:Enter your code:Enter your code:Enter your code:
Floor 4 Posted 2006-09-27 02:01 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Why don't you just put the following code in a batch file to execute? It's quite distracting in the CMD window.

@echo off
for /F %%i in ('dir /ad /s /b') do (echo %%~ni|findstr "123">nul && echo %%~ni)
pause
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-09-27 02:11 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Is your bat name findstr.bat?

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 6 Posted 2006-09-27 02:34 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
LZ wants to achieve what effect??? Don't understand, is it to find the folder named 123? Or something else???
我今后在论坛的目标就是做个超级坏人!!!
Floor 7 Posted 2006-09-27 08:12 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 8 Posted 2006-09-27 09:25 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Originally posted by smileseeker at 2006-9-27 01:18:
D:\scripts>for /F %i in ('dir /ad /s /b') do echo %~ni | findstr "123"

D:\scripts>echo 123 | findstr "123"
Enter your code:Enter your code:Enter your code:Enter yo ...


It is obvious that your code is wrong. dir /ad /s /b is to find folders, and %~ni is expanded to the file name. Where is the file name? There is only the path at all.

Modify the code to:
@echo off
for /F "tokens=*" %%i in ('dir /a-d /s /b *.*') do (
echo %%~ni | findstr "123" && echo Found matching file
)
pause

Please save it as a batch file to run.
Floor 9 Posted 2006-09-27 09:28 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by pengfei at 2006-9-27 09:25:


Obviously your code is wrong. dir /ad /s /b is to find folders, and %~ni is to expand to the file name. Where is the file name? There is only the path at all.

Modify the code as:
@echo off
for /F "t ...
Originally, Windows can't distinguish what is a file and what is a path. Let's ask a question: there is a folder a.txt, do you say it is a folder or a file??? Moreover, how can a machine recognize it
Floor 10 Posted 2006-09-27 09:32 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
I just tested my computer just now and there were no error messages.

I think that %%~ni is expanded to the file name! If it is expanded to a path, an error will occur.

[ Last edited by pengfei on 2006-9-27 at 09:40 ]
Floor 11 Posted 2006-09-27 09:42 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by pengfei at 2006-9-27 09:32:
Just tested my computer and there were no error messages.

I think %%~ni expands to the file name! If it's a path expansion, an error will occur.

[ Last edited by pengfei on 2006-9-27 at 09:40 ]

cd c:\
md test
cd test
md 123
md 123456
for /F %i in ('dir /ad /s /b') do echo %~ni | findstr "123"

Please execute this and see if there will be an error
Floor 12 Posted 2006-09-27 09:44 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by he200377 at 2006-9-27 09:28:

Windows originally can't distinguish what is a file and what is a path. Let's ask a question: there is a folder named a.txt, do you say it is a folder or a file??? Moreover, how can a machine recognize it


  That's not right. The dir command under CMD can distinguish whether a certain file is a folder or a file. Otherwise, why are there the two parameters /ad and /a-d for the dir command? According to your understanding, wouldn't they be useless? Create a folder named a.txt, and try using dir /ad and dir /a-d respectively to see if dir can distinguish them correctly.

  Just in the extension of the for statement, %~xi simply thinks that the last string ending with a dot is the suffix, thus causing the situation of mistakenly thinking that the suffix of the folder a.txt is .txt. If you think that Windows can't distinguish files and paths correctly based on this, that's completely wrong. But this also proves that there is a bug in the extension of the for statement, and you must be careful when using it.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 13 Posted 2006-09-27 09:44 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Yes, it seems I was wrong. It was just theoretical inference without testing.
Floor 14 Posted 2006-09-27 09:52 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Try using dir /ad and dir /a-d respectively. A word to clarify~ It turns out that dir can exclude directories. I was trying to figure out a way to exclude directories last time. This time, I'll go back and check *.exe /?
Floor 15 Posted 2006-09-27 09:53 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by he200377 at 2006-9-27 09:42:

cd c:\
md test
cd test
md 123
md 123456
for /F %i in ('dir /ad /s /b') do echo %~ni | findstr "123"

Please execute this and see if it will go wrong


  From this code, you are executing in the CMD window. Judging from your overall meaning, you want to first go to the root directory of drive C and then create folders, but the first sentence you wrote is not correct because it cannot jump to the root directory of drive C. Think about why; and your final for statement is incomplete, so even if your previous code is all correct, an error will occur when executing here.

  The above code I executed in a batch file by replacing %i with %%i, so the above remarks were wrong. I correct it here and apologize for causing misunderstandings to others.

[ Last edited by namejm on 2006-9-27 at 10:16 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: