Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for which.kix

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

  1. ; Check command line argument
  2. If $search = "" OR InStr( $search, "?" )
  3. 	? "Which.kix,  Version 1.00"
  4. 	? "Locate the specified program file"
  5. 	? "Port of Unix' WHICH command" ?
  6. 	? "Usage:  KIX32  WHICH.KIX  $$SEARCH=filename[.ext]" ?
  7. 	? "Where:  " + Chr(34) + "filename" + Chr(34)
  8. 	" is a file name only: no path, no wildcards" ?
  9. 	? "Written by Rob van der Woude"
  10. 	? "http://www.robvanderwoude.com" ?
  11. 	Exit 2
  12. EndIf
  13.  
  14. ; Store PATH and PATHEXT in arrays
  15. $pathArray    = Split( "@CurDir;%PATH%", ";", -1 )
  16. $pathExtArray = Split( "%PATHEXT%;.DLL;.SYS;.OCX", ";", -1 )
  17.  
  18. ; Reset search result variable
  19. $found = 0
  20.  
  21. ; Search each directory until one match is found
  22. For Each $i In $pathArray
  23. 	If $found = 0
  24. 		$pathDir = $i
  25. 		; Strip trailing backslash and/or dot
  26. 		If Right( $i, 2 ) = "\."
  27. 			$pathDir = SubStr( $i, 1, Len( $i ) - 2 )
  28. 		Else
  29. 			If Right( $i, 1 ) = "\"
  30. 				$pathDir = SubStr( $i, 1, Len( $i ) -1 )
  31. 			EndIf
  32. 		EndIf
  33. 		; Try without appending extension first
  34. 		If InStr( $search, "." )
  35. 			If Exist( $pathDir + "\" + $search )
  36. 				$result = $pathDir + "\" + $search
  37. 				$found  = 1
  38. 			EndIf
  39. 		Else
  40. 			; Then try all the extensions from PATHEXT
  41. 			For Each $j In $pathExtArray
  42. 				If Exist( $pathDir + "\" + $search + $j )
  43. 					$result = $pathDir + "\" + $search + $j
  44. 					$found  = 1
  45. 				EndIf
  46. 			Next
  47. 		EndIf
  48. 	EndIf
  49. Next
  50.  
  51. ; Display the result
  52. If $found = 0
  53. 	? "-None-" ?
  54. 	$ret = 1
  55. Else
  56. 	? $result ?
  57. 	$ret = 0
  58. EndIf
  59.  
  60. ; Exit with return code
  61. Exit $ret
  62.  

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