<http://www.netikka.net/tsneti/info/tscmd185.php>
Copyright © 2003-2012 by Prof. Timo Salmi  
Last modified Mon 23-Apr-2012 06:15:35
Welcoming the user from 23.22.136.56
On Sun 17-Dec-2017 21:43:28 server time

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

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.

References/Comments:
  Google Groups Thread: filespec matches both long and short names
  Google Groups Nov 1 2011, 1:14 am (A VBScript pointer)