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 Windows version
  3. IF "%OS%"=="Windows_NT" (SETLOCAL) ELSE (GOTO Syntax)
  4.  
  5. :: Check command line argument
  6. IF     "%~1"=="" GOTO Syntax
  7. IF NOT "%~2"=="" GOTO Syntax
  8. ECHO.%1 | FIND /V ":" | FIND /V "\" | FIND /V "*" | FIND /V "?" | FIND /V "," | FIND /V ";" | FIND /V "/"  | FIND "%~1" >NUL
  9. IF ERRORLEVEL 1 GOTO Syntax
  10.  
  11. :: If the specified command contains a dot, it is not a macro nor an internal command
  12. ECHO.%1 | FIND "." >NUL
  13. IF NOT ERRORLEVEL 1 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. 		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. 			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. 				GOTO:EOF
  46. 			)
  47. 		)
  48. 	)
  49. )
  50.  
  51. :ExtCmd
  52. :: Search current directory first, then PATH, for the "pure"
  53. :: file name itself or one of the extensions defined in PATHEXT.
  54. :: Add quotes to match directory names with spaces as well.
  55. SET Path="%CD%";"%!!%"
  56. SET Found=-None-
  57. :: This command line was partly rewritten by Yakov Azulay.
  58. FOR %%A IN (%Path%) DO (
  59. 	ECHO.%~1 | FIND "." >NUL
  60. 	IF ERRORLEVEL 1 (
  61. 		FOR %%B IN (%PathExt%) DO (
  62. 			IF EXIST "%%~A.\%~1%%~B" (
  63. 				CALL :Found "%%~A.\%~1%%~B"
  64. 			)
  65. 		)
  66. 	) ELSE (
  67. 		FOR %%B IN (.;%PathExt%) DO (
  68. 			IF EXIST "%%~A.\%~1%%~B" (
  69. 				CALL :Found "%%~A.\%~1%%~B"
  70. 			)
  71. 		)
  72. 	)
  73. )
  74.  
  75. :: Display the result
  76. ECHO.
  77. ECHO.%Found%
  78.  
  79. :: Done
  80. GOTO End
  81.  
  82.  
  83. :Found
  84. :: Stop after finding the first match
  85. IF NOT "%Found%"=="-None-" GOTO:EOF
  86. :: Store the first match found
  87. SET Found=%~f1
  88. GOTO:EOF
  89.  
  90.  
  91. :Syntax
  92. ECHO.
  93. ECHO WHICH, Version 5.20
  94. ECHO UNIX-like WHICH utility for Windows 2000 and later
  95. ECHO.
  96. ECHO Usage:  WHICH  program_name
  97. ECHO.
  98. ECHO Notes:  You may specify the program_name with or without extension, but
  99. ECHO         wildcards, drive, or path are NOT allowed.
  100. ECHO         This batch file first searches the list of DOSKEY macros, then
  101. ECHO         the list of CMD's internal commands, then the current directory,
  102. ECHO         and finally the PATH for the command specified.
  103. ECHO         If SysInternals' STRINGS is available and in the PATH, CMD's internal
  104. ECHO         commands will be verified.
  105. ECHO         If STRINGS is NOT available, internal commands are NOT verified, e.g.
  106. ECHO         even though MKLINK is not available in XP at all, the command
  107. ECHO         "WHICH MKLINK" would still return "-CMD internal command-" anyway.
  108. ECHO.
  109. ECHO Written by Yakov Azulay and Rob van der Woude
  110. ECHO http://www.robvanderwoude.com
  111.  
  112. :End
  113. IF "%OS%"=="Windows_NT" ENDLOCAL
  114.  

page last modified: 2024-04-16; loaded in 0.0075 seconds