Searching for files through a list of directories

Sometimes I need to understand which file, among the ones on my hard disk having the same name, is selected for a given task. A couple of example will better explain what I mean.

Just imagine you are compiling a C/C++ application and you want to know which "common.h" gets included among the many files with that name you have on your hard drive. Or, you are linking an application to library "common.lib" and you want to understand which file, among the many ones with that name, is selected by the linker. Or, last but not least, you run "find" but it doesn't do what it is expected to do and you want to know which of the many find.exe applications is getting executed.

These examples all fall into the more general problem of understanding which file is chosen when there is an environment variable that holds a list of directories (INCLUDE, LIB, PATH, and so on) where that file is to be searched for.

You can do it by hand by searching all directories (in the order they are in the environment variable) for the first occurrence of that file, or have a simple Perl script that does the job for you.

Just in case you don't feel like searching the file by hand (or you don't have time to write your own script) you can find it here. It's been developed on Windows but it should work on Linux/UNIX as well.

Here are some more information:

    search - search directories contained in an environment variable for

    search options file ...

       --var   env. var. containing directories to be searched
       --help  brief help message
       --man   full documentation

    --var    Environment variable containing a list of directories to be

    --help   Print a brief help message and exits.

    --man    Prints the manual page and exits.

    This program will search directories contained in the environment
    variable specified for the given input file(s). Files appearing in more
    that one directory are printed in the order the directory appear in the
    environment variable.


       C:\> search --var PATH grep.exe find.exe

    Directories contained in the environment variable must be separated
    through ';' on Windows system and ':' on UNIX systems.

You can download the script by clicking on the link in the "attachments" section below.

search.bat1.98 KB