Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Sat 10-Apr-2010 22:38:57

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.

46} Is a program available in the default folder or at path?

  @echo off & setlocal enableextensions disabledelayedexpansion
  set find_=gawk.exe
  set found_=
  for %%f in ("%find_%") do set found_="%%~$PATH:f"
  if exist "%find_%" set found_="%find_%"
  if [%found_%]==[""] (
    echo "%find_%" not found at path or in the current folder
    goto :EOF)
  echo Found %found_%
  endlocal & goto :EOF

The output would be e.g.
  Found "c:\_F\FTOOLS\gawk.exe"
  "gawk.exe" not found at path or in the current folder

Or, written as a subroutine
  @echo off & setlocal enableextensions
  call :IsAtPath "gawk.exe" file_
  if not defined file_ echo File not found
  if defined file_ echo Found %file_%
  endlocal & goto :EOF
  :: ==========================================================

  :IsAtPath SearchFor found_
  setlocal enableextensions disabledelayedexpansion
    set found_=
    for %%f in ("%~1") do set found_="%%~$PATH:f"
    if exist "%~1" set found_="%~1"
    if [%found_%]==[""] set found_=
  endlocal & set "%~2=%found_%" & goto :EOF
It would be advisable to avoid poison characters in the file names to be searched for.

It can be considered a counterpart of the UNIX "which" command.
  @echo off & setlocal enableextensions enabledelayedexpansion
  if [%1]==[] (
    echo Usage: %~0 [Filename]
    goto :EOF)
  if not [%2]==[] (
    echo Usage for long file names: %~0 ["Filename"]
    goto :EOF)
  set target_=%~1
  for %%f in ("%target_%") do set where_="%%~$PATH:f"
  if exist "%target_%" set where_="%cd%\%target_%"
  if [%where_%]==[""] (
    echo "%target_%" not found at path or current folder
    ) else (
    set where_=!where_:"=!
    echo where_=!where_!)
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq ssh2.exe
  where_=C:\Program Files\SSH Communications Security\SSH Secure Shell\ssh2.exe

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Sep 10 2005, 8:49 am [M]
  Google Groups Sep 14 2005, 6:31 pm [M]