C:\_G\WWW\~ELISANET\INFO\tscmd042.html
<http://www.elisanet.fi/tsalmi/info/tscmd042.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 30-Nov-2018 11:00:41

 
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.



42} What is the (sub)routine for testing a filename for wildcards?

Based on the comment given in the thread referred to in the " References/Comments " at the and of this item
  @echo off & setlocal enableextensions
  set filename_="My * file"
  echo %filename_%|findstr "[*?]">nul
  if %errorlevel% EQU 0 (
    echo %filename_% contains wildcards
    ) else (
    echo No wildcards in %filename_%)
  endlocal & goto :EOF

Note that this also could be written as
  @echo off & setlocal enableextensions
  set filename_="My * file"
  echo %filename_%|findstr "[*?]">nul ^
    && echo %filename_% contains wildcards^
    || echo No wildcards in %filename_%
  endlocal & goto :EOF

The output would obviously be
  C:\_D\TEST>cmdfaq
  "My * file" contains wildcards

Another take, written with a subroutine and FIND instead of FINDSTR.
  @echo off & setlocal enableextensions
  ::
  :: Demo

  set filename_=My * file
  call :IsWildSub "%filename_%" isWild
  if "%isWild%"=="yes" (
    echo "%filename_%" contains wildcards
    ) else (
    echo No wildcards in "%filename_%")
  ::
  set filename_=My file
  call :IsWildSub "%filename_%" isWild
  if "%isWild%"=="yes" (
    echo "%filename_%" contains wildcards
    ) else (
    echo No wildcards in "%filename_%")
  endlocal & goto :EOF
  ::
  :: =====================================================
  :: Does a name contain wildcards * and/or ?

  :IsWildSub
  setlocal enableextensions
  echo %1|find "*">nul
  if %errorlevel% EQU 0 (endlocal & set %2=yes& goto :EOF)
  echo %1|find "?">nul
  if %errorlevel% EQU 0 (endlocal & set %2=yes& goto :EOF)
  endlocal & set %2=no& goto :EOF

For another example see IsWild in Item #18.

There is a somewhat unrelated aside to wildcards in MS scripting which differs from the usage which UNIX users in particular are accustomed to. Consider a folder containing the following files.
  a.txt
  ab.txt
  abc.txt
  abcd.txt
As expected
  dir /b *.txt
will give
  a.txt
  ab.txt
  abc.txt
  abcd.txt
however,
  dir /b ??.txt
will give
  a.txt
  ab.txt
i.e. the ?? will match both one and two characters. If one wishes to match exactly two characters, then one has to use something like
  dir /b "C:\_M\??.txt"|findstr /i "..\.txt"
which will give
  ab.txt

What about two or more characters?
  dir /b "C:\_M\*.txt"|findstr /i "..\.txt"
which will give
  ab.txt
  abc.txt
  abcd.txt

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Dec 25 2003, 11:35 pm [M]
  hh ntcmds.chm::/ntcmds_shelloverview.htm [You can't use this unless you have the ntcmds.chm file]
  Google Groups Thu, 20 Jan 2011 21:18:09 +0200 [M]

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd042.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd042
http://www.elisanet.fi/tsalmi/info/tscmd042.html
file:///c:/_g/www/~elisanet/info/tscmd042.html