<http://www.netikka.net/tsneti/info/tscmd123.htm>
Copyright © 2003-2011 by Prof. Timo Salmi  
Last modified Mon 23-May-2011 21:37:11

 
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.



123} How to test if a file is in a folder or below two or more times?

  @echo off & setlocal enableextensions enabledelayedexpansion
  set startat_=C:\_M
  set filename_=temp48.prn
  ::
  set count_=0
  for /f %%f in ('dir /b /s "%startat_%\%filename_%"') do (
    set /a count_+=1)
  ::
  echo Found %count_% occurrences of %filename_%
  if %count_% GTR 1 (
    for /f %%f in ('dir /b /s "%startat_%\%filename_%"') do (
      set filename_=%%~dpnxf
      set filedate_=%%~tf
      set filesize_= %%~zf
      set filesize_=!filesize_:~-10!
      echo !filedate_! !filesize_! !filename_!))
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq
  Found 2 occurrences of temp48.prn
  29.10.2005 07:35       1705 C:\_M\temp48.prn
  29.10.2005 07:35       1705 C:\_M\TEMP\TEMP48.PRN

There is a catch, however. Since enabledelayedexpansion has been applied, the file names with exclamation marks would miss the exclamation mark! To circumvent this dilemma, use a subroutine call instead.
  @echo off & setlocal enableextensions
  set startat_=C:\_M
  set filename_=temp48.prn
  ::
  set count_=0
  for /f %%f in ('dir /b /s "%startat_%\%filename_%"') do (
    set /a count_+=1)
  ::
  echo Found %count_% occurrences of %filename_%
  if %count_% GTR 1 (
    for /f %%f in ('
      dir /b /s "%startat_%\%filename_%"') do (
        call :ShowInfo "%%~ff"))
  endlocal & goto :EOF
  ::
  :: =======================================================

  :ShowInfo
  setlocal enableextensions disabledelayedexpansion
    set filename_=%~dpnx1
    set filedate_=%~t1
    set filesize_= %~z1
    set filesize_=%filesize_:~-10%
    echo %filedate_% %filesize_% %filename_%
  endlocal & goto :EOF