Skip to content

Instantly share code, notes, and snippets.

@fwg
Created June 14, 2021 10:26
Show Gist options
  • Save fwg/ef2b28d06aee83f4823e69e026fa859d to your computer and use it in GitHub Desktop.
Save fwg/ef2b28d06aee83f4823e69e026fa859d to your computer and use it in GitHub Desktop.
Windows which
@echo off
php %USERPROFILE%\bin\winwhich.php %*
<?php
$PATH = explode(';', getenv('PATH'));
$PATHEXT = explode(';', getenv('PATHEXT'));
$showCandidates = false;
$file = '';
array_shift($argv);
foreach ($argv as $arg) {
if ($arg == '--show-candidates') {
$showCandidates = true;
} else {
$file = $arg;
}
}
if (empty($file)) {
echo 'No file given!', PHP_EOL;
return 1;
}
array_unshift($PATHEXT, '');
$candidates = array();
foreach ($PATHEXT as $ext) {
$fn = $file . $ext;
foreach ($PATH as $p) {
$candidates[] = $p . '\\' . $fn;
}
}
foreach ($candidates as $c) {
$exists = file_exists($c);
if ($showCandidates) {
echo ($exists ? 'yes' : 'no '), ' ', $c, PHP_EOL;
} elseif (!$showCandidates && $exists) {
echo $c, PHP_EOL;
}
}
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment