Páginas

2011/05/09

PHP: WHICH with PHP on Windows

Which is a very useful linux command that allows you to determine the absolute path of an executable that is contained in path. Futhermore if the executable to launch is inside several folders on the path, it will show which one of those has priority and would be executed.

In order to be able to execute insde a console, you only have to create the which.bat file with the contents I included here and put it on the PHP interpreter's folder. Of course, you will need to add that folder to the PATH enviorement variable.

which.bat
@"%~dp0\php.exe" -r"$cmd_to_check = $argv[1]; $exts = array('exe', 'bat', 'cmd'); foreach (explode(';', getenv('PATH')) as $path) { foreach ($exts as $ext) { $file = $path . '\\' . $cmd_to_check . '.' . $ext; if (is_file($file)) { echo $file; exit; } } } echo $test . ' not found';" %*

Expanded code:
<?php
$cmd_to_check = $argv[1];
$exts = array('exe', 'bat', 'cmd');
foreach (explode(';', getenv('PATH')) as $path) {
    foreach ($exts as $ext) {
        $file = $path . '\\' . $cmd_to_check . '.' . $ext;
        if (is_file($file)) {
            echo $file;
            exit;
        }
    }
}

Usage:
C:\>which php
c:\php\php.exe

No comments:

Post a Comment