X

Help Articles are product support tips and information straight from the NV5 Geospatial Technical Support team developed to help you use our products to their fullest potential.



8204 Rate this article:
No rating

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
Please login or register to post comments.
Featured

End-of-Life Policy Enforcement for ENVI 5.3 / IDL 8.5 and Earlier Versions

5/6/2024

April 1, 2024 Dear ENVI/IDL Customer,  We are reaching out to notify you of our supported... more »

How to Upgrade licenses to ENVI 6.x / IDL 9.x

12/5/2023

What is the new Upgrade function? Starting with ENVI 6.0 and IDL 9.0, we have implemented an... more »

What to do if the 'License Administrator - License Server' for the Next-Generation License Server does not start?

6/13/2023

Background: With the release of ENVI 5.7 & IDL 8.9 and the corresponding Next-Generation licensing... more »

Next-Generation Licensing FAQ

4/28/2023

  NV5 Geospatial has adopted a new licensing technology for all future releases of our ENVI, IDL... more »

The IDL Virtual Machine

6/6/2013

What is the IDL Virtual Machine? An IDL Virtual Machine is a runtime version of IDL that can... more »