Re: Dir undocumented wildcards
Posted: 25 Jan 2015 00:48
The non-standard wildcards work with FINDSTR, and they have an interesting effect that could be really useful
Normally, FINDSTR prefixes each matching line of output with the file name if it searches multiple files due to wildcards, or multiple named files.
But if the file mask includes a non-standard wildcard then the file name prefix is suppressed:
This can be very useful I've seen multiple requests where people want to use FINDSTR to extract lines from multiple files and put the result in a single file. Normally a solution requires either a normal FOR to loop through the files and run FINDSTR on each file, or else FOR /F to process the result of a single FINDSTR with wildcards and strip out the prefix.
But the non-standard wildcards provide a simple solution with a single FINDSTR, and no need for FOR.
Dave Benham
Normally, FINDSTR prefixes each matching line of output with the file name if it searches multiple files due to wildcards, or multiple named files.
Code: Select all
C:\test>for %n in (1 2 3) do @(echo ignore&echo file = "test%n.txt"&echo ignore) >test%n.txt
C:\test>type test?.txt
test1.txt
ignore
file = "test1.txt"
ignore
test2.txt
ignore
file = "test2.txt"
ignore
test3.txt
ignore
file = "test3.txt"
ignore
C:\test>findstr /v ignore "test?.txt"
test1.txt:file = "test1.txt"
test2.txt:file = "test2.txt"
test3.txt:file = "test3.txt"
But if the file mask includes a non-standard wildcard then the file name prefix is suppressed:
Code: Select all
C:\test>findstr /v ignore "test>.txt"
file = "test1.txt"
file = "test2.txt"
file = "test3.txt"
This can be very useful I've seen multiple requests where people want to use FINDSTR to extract lines from multiple files and put the result in a single file. Normally a solution requires either a normal FOR to loop through the files and run FINDSTR on each file, or else FOR /F to process the result of a single FINDSTR with wildcards and strip out the prefix.
But the non-standard wildcards provide a simple solution with a single FINDSTR, and no need for FOR.
Dave Benham