Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for which.bat

(view source code of which.bat as plain text)

  1. @ECHO OFF
  2. :: Check for NT4 or later
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. :: Check for XP or later
  5. FOR /F "tokens=2 delims=[]" %%A IN ('VER') DO FOR /F "tokens=2" %%B IN ("%%~A") DO IF %%B LSS 5.1 GOTO Syntax
  6.  
  7. :: Check command line argument
  8. IF     "%~1"=="" GOTO Syntax
  9. IF NOT "%~2"=="" IF /I NOT "%~2"=="/A" GOTO Syntax
  10. ECHO.%1 | FINDSTR /R /C:"[:\\\*\?,;/]" >NUL && GOTO Syntax
  11.  
  12. :: If the specified command contains a dot, it is not a macro nor an internal command
  13. ECHO.%1 | FIND "." >NUL && GOTO ExtCmd
  14.  
  15. :: Check if the command specified is a DOSKEY macro
  16. FOR /F "tokens=1* delims==" %%A IN ('DOSKEY /MACROS 2^>NUL') DO (
  17. 	IF /I "%~1"=="%%~A" (
  18. 		ECHO.
  19. 		ECHO -DOSKEY Macro-
  20. 		IF /I NOT "%~2"=="/A" GOTO:EOF
  21. 	)
  22. )
  23.  
  24. :: Next, check if the command specified is an internal command; to do so
  25. :: reliably we need SysInternals' STRINGS; if it isn't available, we'll
  26. :: just check against a list of known internal commands, risking "false
  27. :: positives" in older CMD versions (e.g. MAKELINK does not exist in XP,
  28. :: but without STRINGS.EXE this batch file will still return "-CMD
  29. :: internal command-"; when STRINGS.EXE IS available, the batch file would
  30. :: return "-None-")
  31. FOR %%A IN (APPEND ASSOC BREAK CALL CD CHCP CHDIR CLS COLOR COPY DATE DEL DIR DPATH ECHO ENDLOCAL ERASE EXIT FOR FTYPE GOTO IF KEYS MD MKDIR MKLINK MOVE PATH PAUSE POPD PROMPT PUSHD RD REM REN RENAME RMDIR SET SETLOCAL SHIFT START TIME TITLE TRUENAME TYPE VER VERIFY VOL) DO (
  32. 	IF /I "%~1"=="%%~A" (
  33. 		STRINGS.EXE /? >NUL 2>&1
  34. 		IF ERRORLEVEL 1 (
  35. 			REM * * * NOT TESTED :: may return a false positive * * *
  36. 			ECHO.
  37. 			ECHO -CMD Internal Command-
  38. 			IF /I NOT "%~2"=="/A" GOTO:EOF
  39. 		) ELSE (
  40. 			REM * * * TESTED with STRINGS.EXE :: might still occasionally fail, though * * *
  41. 			STRINGS.EXE "%ComSpec%" | FINDSTR.EXE /R /B /I /C:"%~1$" >NUL
  42. 			IF NOT ERRORLEVEL 1 (
  43. 				ECHO.
  44. 				ECHO -CMD Internal Command-
  45. 				IF /I NOT "%~2"=="/A" GOTO:EOF
  46. 			)
  47. 		)
  48. 	)
  49. )
  50.  
  51. :ExtCmd
  52. SETLOCAL ENABLEDELAYEDEXPANSION
  53.  
  54. :: Search current directory first, then PATH, for the "pure"
  55. :: file name itself or one of the extensions defined in PATHEXT.
  56. :: Add quotes to match directory names with spaces as well.
  57. :: This command line was partly rewritten by David Riemens.
  58. SET SRCH_PATH=.\;%PATH%
  59. SET Found=-None-
  60. FOR %%A IN (.;%PathExt%) DO (
  61. 	IF "!Found!"=="-None-" (
  62. 		FOR %%B IN ("%~1%%~A") DO (
  63. 			IF NOT "%%~$SRCH_PATH:B"=="" (
  64. 				FOR %%C IN ("%%~$SRCH_PATH:B") DO SET Test=%%~xC
  65. 				IF NOT "!Test!"=="" IF NOT "!Test!"=="." (
  66. 					ECHO.%%~$SRCH_PATH:B
  67. 					IF /I NOT "%~2"=="/A" (
  68. 						ENDLOCAL
  69. 						GOTO:EOF
  70. 					)
  71. 				)
  72. 			)
  73. 		)
  74. 	)
  75. )
  76.  
  77. :: No success so far...?
  78. IF /I NOT "%~2"=="/A" ECHO -None-
  79.  
  80. :: Done
  81. ENDLOCAL
  82. GOTO:EOF
  83.  
  84.  
  85. :Syntax
  86. ECHO WHICH, Version 6.11
  87. ECHO UNIX-like WHICH utility for Windows XP and later
  88. ECHO.
  89. ECHO Usage:    WHICH  progname  [ /A ]
  90. ECHO.
  91. ECHO Where:    progname    is the name of the program to locate
  92. ECHO           /A          lists all occurrences, not just the first one
  93. ECHO.
  94. ECHO Returns:  "-DOSKEY Macro-", "-CMD Internal Command-", or the fully qualified
  95. ECHO           path to the first matching program found in the PATH, based on the
  96. ECHO           specified extension or the ones listed in PATHEXT; or "-None-".
  97. ECHO.
  98. ECHO Notes:    progname MAY include extension; wildcards, drive, path NOT allowed.
  99. ECHO           This batch file first searches the list of DOSKEY macros, then CMD's
  100. ECHO           internal commands, then current directory, finally PATH for progname.
  101. ECHO           Only if SysInternals' STRINGS is available and in the PATH, CMD's
  102. ECHO           internal commands will be verified; e.g. even though MKLINK is not
  103. ECHO           available in XP at all, without STRINGS the command "WHICH MKLINK"
  104. ECHO           would still return "-CMD internal command-" anyway.
  105. ECHO           When the /A switch is used, nothing is returned when nothing is found
  106. ECHO           (unlike without /A switch, where "-None-" would be returned).
  107. ECHO.
  108. ECHO Written by David Riemens and Rob van der Woude
  109. :: The things you need to do to make the text fit in 80 columns and 25 lines...
  110. IF NOT "%OS%"=="Windows_NT" ECHO http://www.robvanderwoude.com
  111. IF     "%OS%"=="Windows_NT" SET /P "=http://www.robvanderwoude.com" < NUL
  112. IF     "%OS%"=="Windows_NT" EXIT /B 1
  113.  

page last uploaded: 2017-08-21, 14:26