Directory Files
Function Syntax | (LM:directoryfiles <dir> [typ] [sub]) |
Current Version | 1.0 |
Donate |
Arguments | ||
---|---|---|
Symbol | Type | Description |
dir | String | Root directory for which to return filenames |
typ | String | [Optional] Filter for filetype (DOS pattern e.g. "*.dwg") |
sub | Boolean | [Optional] If T, subdirectories of the root directory are included |
Returns | ||
Type | Description | |
List | List of files matching the filetype criteria, else nil if none are found |
Function Description
This function will retrieve a list of all filenames or filenames of a specific filetype residing in a given root directory (and, optionally, subdirectories of the given directory).
The function is essentially a wrapper for the AutoLISP vl-directory-files function, however, where the vl-directory-files function is limited to returning a list files found in a given directory, this function will optionally include files residing in all subdirectories of the given directory.
;; Directory Files - Lee Mac ;; Retrieves all files of a specified filetype residing in a directory (and subdirectories) ;; dir - [str] Root directory for which to return filenames ;; typ - [str] Optional filetype filter (DOS pattern) ;; sub - [bol] If T, subdirectories of the root directory are included ;; Returns: [lst] List of files matching the filetype criteria, else nil if none are found (defun LM:directoryfiles ( dir typ sub ) (setq dir (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dir))) (append (mapcar '(lambda ( x ) (strcat dir "\\" x)) (vl-directory-files dir typ 1)) (if sub (apply 'append (mapcar '(lambda ( x ) (if (not (wcmatch x "`.,`.`.")) (LM:directoryfiles (strcat dir "\\" x) typ sub) ) ) (vl-directory-files dir nil -1) ) ) ) ) )
Example Function Call
The following example will list all drawing files in the working directory:
(LM:directoryfiles (getvar 'dwgprefix) "*.dwg" nil)
Example Program
The following example program will prompt the user to select a directory and will create a text file in the selected directory containing a list of all drawing files found in the selected directory & subdirectories of the selected directory.
Note: The following program requires my Browse for Folder function to be loaded before running.
(defun c:listfiles ( / des dir lst txt ) (cond ( (null (setq dir (LM:browseforfolder "Select a directory to list drawing files" nil 0))) (princ "\n*Cancel*") ) ( (null (setq lst (LM:directoryfiles dir "*.dwg" t))) (princ "\nNo drawing files found in the selected directory or subdirectories.") ) ( (null (setq txt (vl-filename-mktemp "dwgs" dir ".txt") des (open txt "w"))) (princ "\nUnable to open text file for writing.") ) ( (foreach dwg lst (write-line dwg des)) (close des) (princ (strcat "\n" (itoa (length lst)) " drawing(s) written to " txt)) ) ) (princ) )