/*-------------------------------------------------------------------------------
  retrievefilenames.ado: retrieve filenames from filesystem directory

    Copyright (C) 2010-2018  Daniel Bela (daniel.bela@lifbi.de)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

-------------------------------------------------------------------------------*/
*! retrievefilenames.ado: retrieve filenames from filesystem directory
*! Daniel Bela (daniel.bela@lifbi.de), Leibniz Institute for Educational Trajectories (LIfBi), Germany
*! version 1.6 14 September 2018 - add deprecation notice in favor of -filesearch.ado-
*! version 1.5 17 June 2013 - corrected quotes in returned values
*! version 1.4 19 January 2012 - fixed sort order of results
*! version 1.3 12 October 2011 - hardened procedure against filenames containing spaces
*! version 1.2 30 June 2011 - added option 'local', inspired by Stata's 'levelsof' command
*! version 1.1 22 December 2010 - initial release
program define retrievefilenames, nclass
	version 11.1
	noisily : display as error in smcl `"Thanks for using {cmd:retrievefilenames}. This program is deprecated in favor of its more flexible successor, {cmd:filesearch}."'_newline ///
		`"{tab}{cmd:filesearch} is available from {stata ssc describe filesearch:SSC}, or from LIfBi's self-operated {stata "net describe filesearch , from(http://nocrypt.neps-data.de/stata)":Stata repository}."'_newline ///
		`"{tab}Please consider migrating your syntax. The {help filesearch:help file for filesearch} can help (if installed)."'
	syntax [, EXTension(string) SUBDIRectories FILEdirectory(string) SEARCHexpression(string) Local(name) STRIP]
	if (!missing(`"`extension'"')) {
		if !missing("`searchexpression'") {
			noisily: display as error `"both {it:searchexpression} and {it:extension} specified; option {it:searchexpression} ignored"'
		}
		local extension=regexr("`extension'","^\.","")		// remove leading period if necessary
		local pattern "\.`extension'$"
	}
	else {
		local pattern : copy local searchexpression
	}
	if (missing(`"`pattern'"')) local pattern .*
	filesearch `pattern' , `subdirectories' `strip' regexpression local(`local') directory(`filedirectory') maxdepth(0)
	if (!missing(`"`local'"')) c_local `local' : copy local `local'
	exit 0
end
// EOF