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.
C:\_D\TEST>cmdfaq
Found "c:\_F\FTOOLS\gawk.exe"
or
"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