This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
185} How to get DIR wildcards match long file names only?
First, what is the problem here? Consider the following four files in a
folder:
Eng [s01 1986].txt
Eng_[s02 1986].txt
Eng_[s03 1986].txt
Eng [s04 1986].txt
Given
C:\TEST>dir /b Eng_*.txt
The output will be:
Eng [s01 1986].txt
Eng_[s02 1986].txt
Eng_[s03 1986].txt
Eng [s04 1986].txt
That's obviously not what we expect and wish. So, why then is also
first and the last filename matched? The reason is in the SFN i.e. the
short file names:
C:\TEST>dir /x Eng_*.txt
reveals
ENG_S0~1.TXT Eng [s01 1986].txt
ENG_S0~2.TXT Eng [s04 1986].txt
ENG__S~1.TXT Eng_[s02 1986].txt
ENG__S~2.TXT Eng_[s03 1986].txt
where all the SFNs in the example are matched by the
Eng_*.txt
What to do about it? Utilize findstr regular expressions. E.g.
@echo off & setlocal enableextensions
set dir_=C:\TEST
pushd "%dir_%"
for /f "usebackq delims=" %%f in (
`dir /b /a:-d-s-h /o:n "*.*"^|
findstr /b /e "Eng_.*\.txt"`) do (
echo %dir_%\%%f)
popd
endlocal & goto :EOF
which, as desired, gives
C:\TEST\Eng_[s02 1986].txt
C:\TEST\Eng_[s03 1986].txt
The FOR, then, and/or the list produced by it, can be used as a basis
for whatever operations one wishes to perform on the matched files.