X
8107

Examples illustrating recursive / nonrecursive search for files from a given set of directories

QUESTION:

How do I programmatically find all the file with a certain file name extension from a given set of directories?

ANSWER:

The FILE_SEARCH routine provides a easy way to do this, where a single call can search a single directory or a set of directories provided as a string array.  FILE SEARCH can search in a given directory or recursively in a subdirectory tree.

    http://www.exelisvis.com/docs/FILE_SEARCH.html

To search recursively through an array of folders for files that match a given filter string, you could use the FILE_SEARCH routine in IDL with two arguments (in this case, an array of directory paths and a file name pattern filter), for example (using Windows paths with IDL 8.4.1):

    dirs = [!dir+'\lib\datatypes', !dir+'\lib\wavelet']
    filter = '*.sav'
    files = file_search(dirs, filter)
    print, files, /implied_print

The result from the above is:

    FILES           STRING    = Array[7]
    C:\Program Files\Exelis\IDL84\lib\datatypes\dictionary__define.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\hash.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\hash__define.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\list.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\list__define.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\orderedhash__define.sav
    C:\Program Files\Exelis\IDL84\lib\wavelet\data\wv_sample.sav

Notice that the search for qualifying files recurses into subdirectories of the folders specified in the first argument, where one of the results is:

    C:\Program Files\Exelis\IDL85\lib\wavelet\data\wv_sample.sav

However, if you don't want a recursive directory search, but only want to search a single level of the specified directories then it is necessary to call FILE_SEARCH with only one argument -- in this case, an array of file name filter paths, where each of the paths includes a file name search filter. For example:

    dirs = [!dir+'\lib\datatypes', !dir+'\lib\wavelet']
    filter = '*.sav'
    fpaths = dirs + path_sep() + filter  ; Append the filter to each path
    files = file_search( fpaths )
    help, files
    print, files, /implied_print

The result from running the above statements is:

    FILES           STRING    = Array[6]
    C:\Program Files\Exelis\IDL84\lib\datatypes\dictionary__define.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\hash.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\hash__define.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\list.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\list__define.sav
    C:\Program Files\Exelis\IDL84\lib\datatypes\orderedhash__define.sav

In this case there is no recursive directory search and so no matching file name from the "wavelet\data" sub-directory is returned by FILE_SEARCH.

 

Reviewed by JU (31-July-2015) ; kk 7/31/15