{"id":34,"date":"2004-11-10T17:47:30","date_gmt":"2004-11-10T17:47:30","guid":{"rendered":"http:\/\/gaisan.com\/wordp\/?p=34"},"modified":"2004-11-10T17:47:30","modified_gmt":"2004-11-10T17:47:30","slug":"which-command-in-dos","status":"publish","type":"post","link":"https:\/\/gaisan.com\/blogs\/?p=34","title":{"rendered":"Which command in DOS"},"content":{"rendered":"<p>I&#8217;ve been told that I should add more of the little programming hints and tips that I used to come up with during my reearch days to this site. Well here&#8217;s something I was playing around with today that&#8217;s useful for many windows developers. Like many programmers I&#8217;m often more comfortable at the command line than using some funky GUI where I have to drag <i>(or learn so many command alias key-strokes that I may aswell be at the console anyway)<\/i>.<br \/>\nI was stuck for a UNIX version of the which command.  According to <a href=\"http:\/\/www.rt.com\/man\/which.1.html\">man which<\/a> this command<\/p>\n<blockquote><p>\n<b>Which<\/b>  takes a series of program names, and prints out the<br \/>\nfull pathname of the program that the shell would call  to<br \/>\nexecute it.  It does this by simulating the shells search-<br \/>\ning of the <b>$PATH<\/b> environment variable.\n<\/p><\/blockquote>\n<p>Replicating this functionality using DOS batch ain&#8217;t that bad&#8230;<br \/>\n<code><br \/>\n@ECHO OFF<br \/>\nrem Sanity check OS version and arguments.<br \/>\nIF \"%OS%\"==\"Windows_NT\" (SETLOCAL) ELSE (GOTO Syntax)<br \/>\nIF     \"%~1\"==\"\" GOTO Syntax<br \/>\nIF NOT \"%~2\"==\"\" GOTO Syntax<br \/>\nECHO.%1 \u00a6 FIND \/V \":\" \u00a6 FIND \/V \"\\\" \u00a6 FIND \/V \"*\" \u00a6 FIND \/V \"?\" \u00a6 FIND \/V \",\" \u00a6 FIND \/V \";\" \u00a6 FIND \/V \"\/\"  \u00a6 FIND \"%1\" >NUL<br \/>\nIF ERRORLEVEL 1 GOTO Syntax<\/code><br \/>\n<code><br \/>\nSET Found=<br \/>\nrem Get the short name for the current directory<br \/>\nCOMMAND \/C REM<br \/>\nrem Search CurrentDir, path and pathext for the file<br \/>\nFOR %%A IN (%CD%;%Path%) DO FOR %%B IN (.;%PathExt%) DO IF EXIST \"%%~A.\\%~1%%~B\" CALL :Found \"%%~A.\\%~1%%~B\"<br \/>\nrem Display the result<br \/>\nECHO.<br \/>\nIF DEFINED Found (ECHO.%Found%) ELSE (ECHO -None-)<br \/>\nrem Done<br \/>\nGOTO End<\/code><br \/>\n<code><br \/>\n:Found<br \/>\nIF DEFINED Found GOTO:EOF<br \/>\nrem Store the first match found<br \/>\nSET Found=%~f1<br \/>\nGOTO:EOF<br \/>\n:Syntax<br \/>\nECHO.<br \/>\nECHO WHICH, Version 2.00<br \/>\nECHO UNIX-like WHICH utility for Windows NT 4 \/ 2000 \/ XP<br \/>\nECHO.<br \/>\nECHO Usage:  WHICH  program_name<br \/>\nECHO.<br \/>\nECHO Specify program_name with or without<br \/>\nECHO extension and without a drive or path.<br \/>\nECHO Just like the UNIX command. (no wildcards please)<\/code><br \/>\n<code><br \/>\n:End<br \/>\nIF \"%OS%\"==\"Windows_NT\" ENDLOCAL<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been told that I should add more of the little programming hints and tips that I used to come up with during my reearch days to this site. Well here&#8217;s something I was playing around with today that&#8217;s useful for many windows developers. Like many programmers I&#8217;m often more comfortable at the command line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/34"}],"collection":[{"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=34"}],"version-history":[{"count":0,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}