############################################################################ # # File: gdl.icn # # Subject: Procedures to get directory lists # # Author: Richard L. Goerwitz # # Date: May 2, 2001 # ############################################################################ # # This file is in the public domain. # ############################################################################ # # Version: 1.3 # ############################################################################ # # Gdl returns a list containing everything in a directory (whose name # must be passed as an argument to gdl). Nothing fancy. I use this file # as a template, modifying the procedures according to the needs of the # program in which they are used. # ############################################################################ # # Requires: UNIX or MS-DOS # ############################################################################ procedure gdl(dir) local getdir getdir := set_getdir_by_os() return getdir(dir) end procedure set_getdir_by_os() # Decide how to get a directory, based on whether we are running # under Unix or MS-DOS. if find("UNIX", &features) then return unix_get_dir else if find("MS-DOS", &features) then return msdos_get_dir else stop("Your operating system is not (yet) supported.") end procedure msdos_get_dir(dir) # Returns a sorted list of all filenames (full paths included) in # directory "dir." The list is sorted. Fails on invalid or empty # directory. Aborts if temp file cannot be opened. # # Temp files can be directed to one or another directory either by # manually setting the variable temp_dir below, or by setting the # value of the environment variable TEMPDIR to an appropriate # directory name. local in_dir, filename_list, line, temp_name, filename static temp_dir initial { temp_dir := (trim(map(getenv("TEMPDIR"), "/", "\\"), '\\') || "\\") | ".\\" } # Get name of tempfile to be used. temp_name := get_dos_tempname(temp_dir) | stop("No more available tempfile names!") # Make sure we have an unambiguous directory name, with backslashes # instead of Unix-like forward slashes. dir := trim(map(dir, "/", "\\"), '\\') # Put dir listing into a temp file. system("dir "||dir||" > "||temp_name) # Put tempfile entries into a list, removing blank- and # space-initial lines. Exclude directories (i.e. return file # names only). in_dir := open(temp_name,"r") | stop("Can't open temp file in directory ",temp_dir,".") filename_list := list() every filename := ("" ~== !in_dir) do { match(" ",filename) | find("