哈哈……这个好玩~:)
经打开 @echo on 之后发现与 Findstr 有关:
运行下面的代码(文件名为: “新建 批处理(2).bat” ):
@echo on
::off
dir/b|findstr /i /v "%~n0.bat"|findstr /n .*
pause
开启 @echo on ,运行代码过程:
dir/b | findstr /i /v "新建 批处理(2).bat" | findstr /n .*
……
……
发现上面被 Findstr 搜索的文件名 “新建 批处理(2).bat” 是一个中间含有空格的文件名,而这个含有空格的文件名也是要被 Findstr 搜索的字符串。
但是, Findstr 的特性是 “字符串A 字符串B” 虽然它们在同一个引号范围之内,但是 Findstr 的特性是会将它们理解为要同时搜索满足上面两个条件,即:只要含有“字符串A”或是含有“字符串B”,那么都符合 Findstr 要搜索的目标。
而将上面引号中的要搜索的文件名正好有空格,所以 Findstr 理解为:查找 “新建” 或是含有 “批处理(2).bat” 的字符串就满足条件,再加上 /V 要滤掉目标,而正在这两个批处理的文件名都含有 “新建” 这个检索的成功条件,结果就被滤掉了。
将文件名改名,去掉中间空格以后,搜索正确,就推断就上面因素~:)
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~ :)