中国DOS联盟论坛

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-08-06 01:18
48,039 topics / 350,124 posts / today 2 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » What's wrong with my findstr
Printable Version  2,969 / 22
Floor1 smileseeker Posted 2006-09-27 01:18
中级用户 Posts 83 Credits 316
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!!!
Floor2 namejm Posted 2006-09-27 01:23
荣誉版主 Posts 1,737 Credits 5,226 From 成都
There is something wrong with your code. Try the following statement:
Floor3 smileseeker Posted 2006-09-27 01:30
中级用户 Posts 83 Credits 316
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:
Floor4 namejm Posted 2006-09-27 02:01
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Why don't you just put the following code in a batch file to execute? It's quite distracting in the CMD window.
Floor5 electronixtar Posted 2006-09-27 02:11
铂金会员 Posts 2,672 Credits 7,493
Is your bat name findstr.bat?
Floor6 9527 Posted 2006-09-27 02:34
银牌会员 Posts 438 Credits 1,185 From 北京
LZ wants to achieve what effect??? Don't understand, is it to find the folder named 123? Or something else???
Floor7 vkill Posted 2006-09-27 08:12
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
Floor8 pengfei Posted 2006-09-27 09:25
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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.
Floor9 vkill Posted 2006-09-27 09:28
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
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
Floor10 pengfei Posted 2006-09-27 09:32
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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 ]
Floor11 vkill Posted 2006-09-27 09:42
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
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
Floor12 namejm Posted 2006-09-27 09:44
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor13 pengfei Posted 2006-09-27 09:44
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
Yes, it seems I was wrong. It was just theoretical inference without testing.
Floor14 vkill Posted 2006-09-27 09:52
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
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 /?
Floor15 namejm Posted 2006-09-27 09:53
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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 ]
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023